我有一个列表,其中包含有关南美洲许多动物可能位置的信息。例如,这是存储信息的类型以及为第一个个体绘制时的外观。
例子:
> s[1]
[[1]]
class : RasterLayer
dimensions : 418, 313, 130834 (nrow, ncol, ncell)
resolution : 0.16666, 0.16666 (x, y)
extent : -86.333, -34.16842, -55.91633, 13.74755 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : in memory
names : layer
values : 0, 1 (min, max)
> plot(s[[1]])
注:绿色区域均为“可能”位置,灰色区域为“不太可能”位置。
我想计算这个可能位置的质心(即绿色区域的质心)。
@dww 下面建议了以下解决方案(适用于模拟数据),但会导致我的数据出现错误消息。
colMeans(xyFromCell(s, which(s[]==1)))
Error in xyFromCell(s[1], which(s[] == 1)) :
trying to get slot "extent" from an object of a basic class ("list") with no slots
最佳答案
找到栅格 r
所在单元格的质心具有值 1
, 您可以使用
colMeans(xyFromCell(r, which(r[]==1)))
本质上,质心位于子集位置的纬度/经度的平均值。
以下是一些可重复的虚拟数据进行测试:
r = raster(matrix(sample(0:1, 10000,T), nrow=100,ncol=100))
关于r - 计算光栅的质心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52652444/