Thursday, 15 July 2010

Calculate average distance between point and closest neighbors in R -



Calculate average distance between point and closest neighbors in R -

i trying calculate average distance between given point , x number of closest neighbors understand how far points neighbors dataset. while using earth.dist() provides total distance matrix between points (and global mean), i'd find distance between point , 5 closest neighbors. example:

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

any , help closest neighbors appreciated.

i sort , take mean of first (other 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

r distance geo

No comments:

Post a Comment