I have a spatial dataframe with about 3000 points. I want to generate a matrix that provides the k (in this case 30) nearest neighbors for each point.
I can do it using a loop but i feel that there should be an elegant and optimal way for spatial points dataframe class that i do not know of.
Probably the fastest is to use
RANNpackage – assuming you havexandy:gives you a 3000 x 30 matrix of closest neighbors. It is several orders of magnitude faster than a naive quadratic search.
Edit: Just for completeness, it doesn’t matter which ANN frontend you pick, with
FNN(suggested by Spacedman) this would be