我有一个包含经度/纬度点的数据集和每组坐标的结果值。我想创建一个空间网格,然后对同一网格中的坐标取结果的平均值,并生成一个新的数据框,为每个坐标分配一个网格编号并具有平均结果。例如,从以下代码开始:
require(sp)
require(raster)
frame <- data.frame(x = c(7.5, 8.2, 8.3), y = c(1,4,4.5), z = c(10,15,30))
coordinates(frame) <- c("x", "y")
proj4string(frame) <- CRS("+proj=longlat")
grid <- GridTopology(cellcentre.offset= c(0,0), cellsize = c(2,2), cells.dim = c(5,5))
sg <- SpatialGrid(grid)
poly <- as.SpatialPolygons.GridTopology(grid)
proj4string(poly) <- CRS("+proj=longlat")
plot(poly)
text(coordinates(poly), labels = row.names(poly), col = "gray", cex. =.6)
points(frame$x, frame$y, col = "blue", cex = .8)
然后我想平均网格单元内的结果(z)并生成一个看起来像这样的数据框(例如观察):
x y z grid grid_mean
1 7.5 1.0 10 g20 10
2 8.2 4.0 15 g15 22.5
3 8.3 4.5 30 g15 22.5
感谢您的任何帮助。