Calculate average distance between point and closest neighbors in R

3.6k Views Asked by At

I am trying to calculate the average distance between a given point and x number of its closest neighbors to understand how far points are from their neighbors for a dataset. While using earth.dist() provides the full distance matrix between all points (and global mean), I'd like to find the distance between a point and its 5 closest neighbors. For example:

frame <- data.frame(long = rnorm(100), lat = rnorm(100))
earth.dist(frame)
mean(earth.dist(frame))  # provides the global mean

Any and all help to get to the closest neighbors greatly appreciated.

3

There are 3 best solutions below

0
On BEST ANSWER

I would just sort and take the mean of first (other than its self-distance):

distM <- as.matrix( fossil::earth.dist(frame))
apply( distM, 1, function(x) mean( x[order(x)][2:6] ) )
#----------
        1         2         3         4         5         6         7 
 93.57153  56.06655 129.84690  95.13023  55.96412  70.57303  55.60863 
        8         9        10        11        12        13        14 
111.79244  17.56394  34.10893  21.80423  20.30025  29.57373  31.13890 
snipped
0
On

I figured out how to do this using lapply() too.

distM <- as.matrix( fossil::earth.dist(frame))
unlist(lapply(1:nrow(distM), function(x) mean(distM[x, order(distM[x, ])[2:6]])))
0
On

To get the 5 closest neighbors for each point, you could do

library(fossil)
set.seed(15)
frame <- data.frame(long = rnorm(100), lat = rnorm(100))
ed <- earth.dist(frame)
closen <- apply(as.matrix(ed), 1, function(x) order(x)[1:6][-1])

so the indexes of the closest neighbors for the first point are

closen[,1]
# [1] 41 26 13 75  7