我有一組數據集,其中包含經度/緯度點和每組座標的結果值。我想創建一個空間網格,然後獲取同一網格中的座標結果的平均值,並生成一個新的數據框,爲每個座標分配一個網格數並得到平均結果。例如,從這樣的代碼:分析空間連接的數據(指向柵格)並生成新的數據集R
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)和產生看起來像這樣(.eg觀察)的數據幀:
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
感謝任何和所有的幫助。
代碼你提供的第一點是g20和g25之間的邊界,而不是g10。你確定網格是你想要的嗎?另外,你想如何處理邊界上的點? – jlhoward 2014-09-23 19:26:21
感謝您的支持。我已經在g20上列出了它;對於邊界點,隨機分配將是我的偏好。 – 2014-09-23 19:28:07