best attempt at nearest neighbor

Close, but not quite finished. Got a neighbor from the test data for each item in the training data, but need to then predict a label. Unsure of accuracy of the other work, as a certain value seemed to crop up more often than made sense.
With the apply function, you just add extra arguments using commas. So it would be something like:
apply(distances, 1, add_labels, distances$i, distances$n)

Or perhaps maybe your syntax is allowed and I just didn't know it!