I'm trying to estimate an average value of distances to K nearest neighbors. I have two sets of coordinates and I'm trying to estimate the average distance from each point in one set of coordinates to the K nearest neighbors in the other set of coordinates.

The following post is instructive in how this could be done when the neighbors are from the same set of coordinates: Simple KNN for spatial data

But in my case I have two sets representing different feature types and I would like to know the average distance between each observation of one feature type with the K nearest neighbors of the other feature type.

Any suggestions?

1 Answer 1


You want something like the get.knnx function from the FNN package:

  get.knnx(data, query, k=10, algorithm=c("kd_tree", "cover_tree",
               "CR", "brute"))

It returns the k points in data that are nearest to each point in query.

  • Thanks, this did the trick!
    – T72
    Apr 19, 2022 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.