问题描述
我刚刚开始学习R,但希望项目早日完成。非常简单:我有一个X列和一个Y列,分别由X坐标和Y坐标组成。 (在NAD27坐标系中工作)。从第一个坐标开始,我想在数据集中找到最近的点,然后移到下一个坐标,然后在同一数据集中找到它的最近的点。理想情况下,它将遍历每个点并确定最接近的点。
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
推荐答案
这是使用 RANN的一种方式
软件包。该方法类似于中显示的方法,但是适用于单点集(链接的帖子大约是找到集合A中与集合B中每个点最近的点。
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).
xy <- read.table(text='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', header=TRUE, row.names=1)
library(RANN)
closest <- nn2(data=xy, k=2)[[1]]
以上,我们将您的单点集 xy
提供给 data
参数,并指定我们要 nn2
来找到每个点两个最近的点(因为最近的点是焦点本身)。 nn2
函数返回包含两个元素的列表:每个 k $ c $的索引的向量(在这种情况下为矩阵) c>最近的点(对于每个查询点);以及距离的向量(矩阵)。我假设我们对距离不感兴趣,所以在上面我们将结果子集到第一个元素。
Above, we supply your single set of points, xy
, to the data
argument, and specify that we want nn2
to find the two nearest points to each point (because the nearest point is the focal point itself). The nn2
function returns a list with two elements: a vector (matrix, in this case) of indices of each of the k
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.
closest
## [,1] [,2]
## [1,] 1 8
## [2,] 2 6
## [3,] 3 5
## [4,] 4 7
## [5,] 5 9
## [6,] 6 2
## [7,] 7 4
## [8,] 8 10
## [9,] 9 5
## [10,] 10 8
要获得最近点的坐标矩阵,可以使用:
To get a matrix of coordinates of the nearest points, you could use:
xy[closest[, 2], ]
默认情况下 nn2
使用一棵kd树-您可能要尝试使用 treetype ='bd'
。
By default nn2
uses a kd tree - you might want to experiment with treetype='bd'
.
这篇关于使用R查找最接近的X,Y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!