I'm just starting to learn R but would like project done sooner rather than later. It's rather simple: I have an X column and a Y column consisting of X coordinates and Y coordinates. (Working in the NAD27 coordinate system). Going from the first coordinate, I'd like to find the nearest point within the data set and then move onto the next coordinate and find it's nearest point within the same data set. Ideally, it would go through each point and determine the closest point.
point x y
1 1601774 14544454
2 1616574 14579422
3 1608698 14572922
4 1602948 14572990
5 1607355 14573871
6 1615336 14578178
7 1603398 14574495
8 1605153 14570727
9 1606758 14573845
10 1606655 14570953
Here's one way, using the
RANN
package. The approach is similar to that shown in this post, but is adapted for a single set of points (the linked post was about finding the nearest point in set A to each point in set B).Above, we supply your single set of points,
xy
, to thedata
argument, and specify that we wantnn2
to find the two nearest points to each point (because the nearest point is the focal point itself). Thenn2
function returns a list with two elements: a vector (matrix, in this case) of indices of each of thek
nearest points (for each queried point); and a vector (matrix) of the distances. I'm assuming we're not interested in the distances, so above we subset the result to the first element.For our problem, the result is a two-column matrix giving the index of the queried point in the first column and the index of the nearest point in the second.
To get a matrix of coordinates of the nearest points, you could use:
By default
nn2
uses a kd tree - you might want to experiment withtreetype='bd'
.