Solution (source code)

= Solution

For each $k=1,\ldots,100$, the call to knn.cv performs leave-one-out nearest-neighbour classification and the third line stores the fraction of omitted observations misclassified. Choose a minimizer of ls, then fit the nearest-neighbour classifier with that $k$ to the full data.