问题描述
我有一个列表,其中包含有关南美许多动物的大概位置的信息.例如,这是存储的信息的类型,以及为第一个人绘制的信息的外观.
I have a list (s) containing information on the probable locations of many animals in South America. For example, this is the type of stored information and what it looks like when plotted for the first individual.
示例:
> 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]])
注意:绿色区域均为可能"位置,灰色区域为不太可能"位置.
Note: the green areas are all "likely" locations and the grey areas are "unlikely" locations.
我想计算此可能位置的质心(即绿色区域的质心).
@dww下面建议使用以下解决方案(适用于模拟数据),但会导致我的数据出现错误消息.
Below @dww suggested the following solution (which works for the simulated data), but leads to an error message with my data.
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
的像元质心,可以使用
To find the centroid of the cells where a raster r
has the value 1
, you can use
colMeans(xyFromCell(r, which(r[]==1)))
本质上,质心位于子集位置的纬度/经度的平均值.
Essentially, the centroid is at the mean of the latitudes/longitudes of the subsetted locations.
这里有一些可重现的虚拟数据要测试:
Here's some reproducible dummy data to test on:
r = raster(matrix(sample(0:1, 10000,T), nrow=100,ncol=100))
这篇关于计算栅格的质心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!