Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分析空间连接(指向网格)的数据,并在R中生成新的数据集_R_Spatial_R Grid - Fatal编程技术网

分析空间连接(指向网格)的数据,并在R中生成新的数据集

分析空间连接(指向网格)的数据,并在R中生成新的数据集,r,spatial,r-grid,R,Spatial,R Grid,我有一个包含经度/纬度点的数据集,以及每组坐标的结果值。我想创建一个空间网格,然后取同一网格中坐标的平均结果,并生成一个新的数据帧,为每个坐标分配一个网格编号,并具有平均结果。例如,从以下代码开始: 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") proj4

我有一个包含经度/纬度点的数据集,以及每组坐标的结果值。我想创建一个空间网格,然后取同一网格中坐标的平均结果,并生成一个新的数据帧,为每个坐标分配一个网格编号,并具有平均结果。例如,从以下代码开始:

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)
谢谢你的帮助

您可以使用包
sp
中的
over(…)
函数来完成此操作。据我所知,您根本不需要包
光栅

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
require(sp)

框架您提供的代码将第一个点放在g20和g25之间的边界上,而不是g10中。你确定网格是你想要的吗?另外,您希望如何处理边界上的点??谢谢您的关注。我把它列在g20;对于边界点,随机分配将是我的首选。太好了,谢谢-这正是我想要的。
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