R中邻居的空间数据/计算指标 [英] spatial data / compute metrics on neighbors in R

查看:33
本文介绍了R中邻居的空间数据/计算指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 (xBin, yBin, value) 形式的二维空间数据.例如:

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))

对于每个 bin,我想计算所有相邻 bin 的变量值"的总和.如果一个 bin 的两个索引 - x 和 y 都在当前 bin 的一个单位内,则该 bin 被认为是邻居

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)

我的真实数据有大约 1,000^2 个 bin,我该如何有效地做到这一点?

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 

对于 1000x1000 矩阵,我无法使用 Winsemius 提出的解决方案(Win 7 x64 8GB RAM)在合理的时间内获得结果

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)

这篇关于R中邻居的空间数据/计算指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆