I have a function where I am calculating the index of the minimum distance between a lat/long coordinate (in SpatialPoints
) and another vector of coordinates (also in SpatialPoints
). The function I use to find min dist is:
library(rgeos)
dfdist$mindist <-apply(gDistance(sp1, sp2, byid=TRUE), 1, which.min)
The function above gives me a column mindist
in my pre-existing data frame dfdist
which is an index of the row number of the point where the minimum distance occurs.
I'd like to also find the 2nd min distance, and the 3rd min dist, but am unsure of how to do this with the apply()
. Is there a replacement for the which.min
which will give me the index of the second min? Third min?
which
withrank
? As in:which(rank(your_data)==2)
. Be aware of ties, though. – coffeinjunky