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.
I would just sort and take the mean of first (other than its self-distance):