如何根据R中的网格索引坐标数据? [英] How to index coordinate data according to a grid in R?
问题描述
我有以下范围的坐标数据(x坐标和y坐标):
Xpos:27-1367nm,Ypos:67-1014nm。一个数据集包含大约2500-3500个数据点。
这是此类数据集的标题:
I have coordinate data (x-coordinates and y-coordinates) on a scale between: Xpos: 27-1367nm, Ypos: 67-1014nm. A data set consists of about 2500-3500 data points. Here is the header of such a data set:
XPos YPos
1 29 211
2 31 609
3 33 1001
4 35 508
5 37 424
6 39 584
7 40 378
8 41 204
9 41 444
10 41 872
...
[![绘制数据] [1]] [1 ]
[![Data plotted][1]][1]
现在,我想通过将由大小相等的象限组成的网格应用于R中的数据来为数据点建立索引。结果应为新列 grid_index,包含一个唯一的Quadrant_ID,数据点位于其中(参见图片)。是否有捷径可寻?我想尝试不同的网格单位大小来对数据进行分区,例如象限大小为50nm,100nm,200nm或400nm,矩形的大小为100nm x 200nm或50nm x100nm。
Now I would like to index the data points by applying a grid consisting of equal sized quadrants onto the data in R. The result should be a new column "grid_index" containing a unique quadrant_ID in which the data points are located (see image). Is there an easy way to do this? I would like to try different grid unit sizes to partition the data e.g. quadrants sized 50nm, 100nm, 200nm or 400nm and rectangles sized 100nm x 200nm or 50nm x100nm.
[![数据点索引的网格] [2]] [2]
[![Grid for data pint indexing][2]][2]
[![每个网格象限应具有唯一的ID] [3]] [3]
[![Each grid quadrant should have an unique ID][3]][3]
我将非常感谢您的帮助。
I would be very grateful for any help.
推荐答案
这是使用 findInterval
的方法:
首先建立一个具有适当数量索引的矩阵:
First set up a matrix that has the appropriate number of indices:
pos.matrix <- matrix(1:35,byrow = TRUE, nrow = 5)
pos.matrix
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1 2 3 4 5 6 7
[2,] 8 9 10 11 12 13 14
[3,] 15 16 17 18 19 20 21
[4,] 22 23 24 25 26 27 28
[5,] 29 30 31 32 33 34 35
接下来使用 findInterval
查找所在矩阵的索引。您可以使用 by =
参数控制网格的大小。请注意,矩阵的尺寸必须与 findInterval
中提供的间隔数匹配。我们需要使用 abs
,因为y值在图表上正在减小。
Next use findInterval
to find the indices of the matrix of where it lies. You can control the size of the grid using the by =
argument. Note that the dimensions of the matrix must match the number of intervals provided in findInterval
. We need to use abs
because the y values are decreasing on the graph.
grid <- apply(cbind(findInterval(data[,"XPos"],seq(0,1400,by = 200)),
abs(findInterval(data[,"YPos"],seq(0,1000,by = 200)) - 6)),
MARGIN = 1,
function(x) pos.matrix[x[2],x[1]])
grid[1:25]
[1] 30 34 31 17 19 26 15 31 19 5 18 32 25 25 14 20 22 19 35 2 16 8 29 29 16
plot(NA,xlim = c(0,1400), ylim = c(0,1000), xlab = "XPos", ylab = "YPos", cex.axis = 0.8)
text(data[,1],data[,2], labels = grid, cex = 0.4)
样本数据
set.seed(3)
data <- data.frame(XPos = runif(1000,0,1400), YPos = runif(1000,0,1000))
这篇关于如何根据R中的网格索引坐标数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!