空间数据/对在研发邻居计算指标 [英] spatial data / compute metrics on neighbors in R
问题描述
我的形式(XBIN,要用ybin,值)的二维空间数据。 例如:
I have the 2D spatial data in the form (xBin, yBin, value). e.g.:
DT = data.table(x=c(rep(1,3),rep(2,3),rep(3,3)),y=rep(c(1,2,3),3),value=100*c(1:9))
有关每个箱我想计算的变量价值的总和所有邻国箱。 甲仓被认为是邻居如果其两个指数 - x和y是在一个单元内,从当前二进制
For each bin I want to compute the sum of variable "value" over all neighboring bins. A bin is considered a neighbor if both of its indices - x and y are within one unit from the current bin
例如。对于x = 2,Y = 2,我想计算
e.g. for x=2, y=2, I want to compute
valueNeighbors(x=2,y=2) = value(x=1,y=1)+value(1,2)+value(1,3)
+value(2,1)+value(2,3)
+value(3,1)+value(3,2)+value(3,3)
我的真实数据有〜1000 ^ 2箱,我怎么能做到这一点有效?
My real data has ~1,000^2 bins, how can I do this efficiently?
推荐答案
也许与栅格
X <- matrix(1:20, 4)
r <- raster(X)
r
agg <- as.matrix(focal(r,matrix(1,3,3),sum, pad = T, padValue = 0))
agg
[,1] [,2] [,3] [,4] [,5]
[1,] 14 33 57 81 62
[2,] 24 54 90 126 96
[3,] 30 63 99 135 102
[4,] 22 45 69 93 70
哪种方法更快地为大型数据集?
Which method is the faster for large datasets?
X <- matrix(1:1000000, 1000)
S <- matrix(NA, nrow(X), ncol(X))
r <- raster(X)
system.time(
as.matrix(focal(r,matrix(1,3,3),sum, pad = T, padValue = 0))
)
user system elapsed
0.39 0.08 0.47
通过一个1000×1000矩阵我无法使用提出Winsemius(Win 7的64位8GB RAM)的解决方案reasobable时间内得到的结果。
With a 1000x1000 matrix I was unable to get a result within a reasobable time using the solution proposed by Winsemius (Win 7 x64 8GB RAM)
这篇关于空间数据/对在研发邻居计算指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!