0

経度/緯度のポイントと各座標セットの結果値を含むデータセットがあります。空間グリッドを作成し、同じグリッド内にある座標の結果の平均をとり、各座標にグリッド番号が割り当てられ、平均された結果を持つ新しいデータフレームを生成したいと思います。たとえば、次のコードから始めます。

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

助けてくれてありがとう。

4

1 に答える 1

2

over(...)これには、パッケージ内の関数を使用できますspraster私が見る限り、パッケージはまったく必要ありません。

require(sp)

frame  <- data.frame(x = c(7.5, 8.2, 8.3), y = c(1,4,4.5), z = c(10,15,30))
points <- SpatialPoints(frame)
proj4string(points) <-  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")

# identify grids...
result <- data.frame(frame,grid=over(points,poly))
# calculate means...
result <- merge(result,aggregate(z~grid,result,mean),by="grid")
# rename and reorder columns to make it look like your result
colnames(result) <- c("grid","x","y","z","grid_mean")
result <- result[,c(2,3,4,1,5)]
result
#     x   y  z grid grid_mean
# 1 8.2 4.0 15   15      22.5
# 2 8.3 4.5 30   15      22.5
# 3 7.5 1.0 10   25      10.0

このover(x,y,...)関数は、2 つのオブジェクトをオーバーレイとして比較し、 の各ジオメトリSpatial*のインデックスを含むベクトルを返します。この場合は SpatialPoints オブジェクトであり、SpatialPolygons オブジェクトです。では、 の各ポイントに関連付けられているポリゴン ID (グリッド セル) を識別します。残りは、平均を計算し、平均を元のデータ フレームとマージし、列の名前を変更して並べ替えて、結果が結果のように見えるようにするだけです。yxxyover(...)yx

意味がわからなかったため、コードを少し調整しました。Z 値を含むデータ フレームを作成し、それを SpatialPoints オブジェクトに変換すると、Z 値が破棄されます...

于 2014-09-23T20:49:01.387 に答える