R Spatial Data – Applying KNN for Spatial Data with Two Sets of Coordinates

knnrspatial statisticsspdep

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?

Best Answer

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.

Related Question