Is there a way to get the distances for the second nearest neighbor between two point patterns in R? The spatstat package has a function called nncross but it only applies to the nearest neighbors between two patterns and I need the distances to the second nearest neighbors.
|
The function get.knnx in package FNN can compute the N-nearest neighbours in point patterns.
now nn$nn.index is a matrix such that nn$nn.index[i,j] is the row in x1 of the two nearest neighbours to row i in x2 - sorted so that the nearest is [i,1], and the next neighbour is [i,2]. The function also returns the distances for you, and has some options to compute spatial indexes for very fast searches. |
|||||||||||
|
|
I just discovered that spatstat has a crossdist function.
It takes two point patterns X and Y as inputs, and returns the matrix whose [i,j] entry is the distance from X[i] to Y[j]. To get the second nearest neighbors using crossdist:
I know I already accepted Spacedman's answer but I would like to share how I did it in another way. |
|||
|
|
