Search code examples
rdistancegeo

Calculate average distance between point and closest neighbors in R


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.


Solution

  • 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