Note: origininally posted in Cross Validated but recommendations said it would be a better fit topic wise here.
I am using the knn method in the FNN package for classification but I would like to see the N closest neighbors as opposed to only the top most one. I have tried with different packages (FastKNN and knncat for example) but I can't find a fast function that will do this for you.
This is a similar question (minus the part of the distance matrix): Find K nearest neighbors, starting from a distance matrix
This is what I tried: LINE contains one row of the distance matrix, LINE_N contains the top N neighbors for each prediction
line_n = c()
tmp_min <- order(line)[1:ncol(distance)]
tmp_id <- c()
for (element in tmp_min)
tmp_id <- c(tmp_id, colnames(distance)[element])
for (element in tmp_id){
if (!(element %in% line_n))
line_n<- c(line_n, element)
if (length(line_n) == N)
break
}
line_n
I was wondering if there was an optimized version of it implemented already or if anyone had any ideas on how to make it faster.