I am looking for a well-optimized function that takes a matrix of distances n X n and returns a matrix n X k with indices k nearest neighbors of the i-th datapoint in the i-th row.
I find gazillion different R packages that allow you to do KNN, but they all seem to include distance calculations along with a sorting algorithm within the same function. In particular, for most procedures, the main argument is the original data matrix, not the distance matrix. In my case, I use non-standard distance for mixed types of variables, so I need to separate the sorting problem from the distance calculations.
This is not a completely frightening problem - I could obviously just use the order function inside the loop to get what I want (see my solution below), but this is far from optimal. For example, the sort function with partial = 1:k , when k is small (less than 11), goes much faster, but, unfortunately, returns only the sorted values, and not the desired indexes.
sorting matrix r knn distance
zkurtz
source share