Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 - Fatal编程技术网

在R中,如何在空间网格正方形上平均空间点数据

在R中,如何在空间网格正方形上平均空间点数据,r,spatial,R,Spatial,现在设法解决了问题 ##convert the grid into a polygon## polys <- as.SpatialPolygons.GridTopology(grd) proj4string(polys) <- CRS("+proj=longlat") ##can now use the function over to select the correct points and average them results <- rep(0, length(p

现在设法解决了问题

##convert the grid into a polygon##
polys <- as.SpatialPolygons.GridTopology(grd) 
proj4string(polys) <- CRS("+proj=longlat")

##can now use the function over to select the correct points and average them
results <- rep(0, length(polys))

for(i in 1:length(polys)) {
  results[i] = mean(df$val[which(!is.na(over(x=df,y=polys[i])))])
}
我有一组大约50000个点,它们有坐标和一个与之相关的值。我希望能够将点放置到网格中,平均所有落入网格正方形的点的关联值。因此,我想以一个对象结束,该对象标识每个网格正方形,并给出网格正方形内的平均值

我有一个空间点数据框和一个空间网格对象中的数据,如果这有帮助的话

改进答案:我确实做了一些搜索,对问题的初始状态表示抱歉,我只是设法将问题框在自己的头脑中;以前没必要把它告诉任何人

下面是一些示例数据,希望能更清楚地说明这个问题

##make some data
longi <- runif(100,0,10)
lati <- runif(100,0,10)
value <- runif(500,20,30)

##put in data frame then change to spatial data frame
df <- data.frame("lon"=longi,"lat"=lati,"val"=value)
coordinates(df) <- c("lon","lat")
proj4string(df) <- CRS("+proj=longlat")

##create a grid that bounds the data
grd <- GridTopology(cellcentre.offset=bbox(df)[,1],
cellsize=c(1,1),cells.dim=c(11,11))
sg <- SpatialGrid(grd)
##制作一些数据

longi你的描述充其量是模糊的。请尽量问更具体的答案,最好用代码说明你已经尝试过的。对点数据或单个光栅单元中的单个值求平均值绝对没有意义

我能提供的最佳答案是使用raster extract()将光栅值指定给sp point对象,然后使用tapply()将值聚合到点中的分组值。您可以使用点的坐标来标识单元位置,也可以使用提取返回的单元号(根据下面的示例)

require(光栅)
需要(sp)
#创建示例数据

r你查过了吗?我相信你会发现这是一个重复的问题。如果你做了搜索,结果是空的,那么你应该选择失败的搜索策略,然后发布一个数据示例(不是50K点,可能是50x50)。对不起,这太模糊了。例如,如何定义“单元”?至少,您需要发布示例数据,并向我们展示所需的输出。如果你想发布完整的数据集(我没问题),那么把它上传到某个地方并发布一个链接。谢谢,我想我用的单词cell是错的,我是指网格正方形。我编辑了我的问题,试图澄清。我想根据它们所在的网格正方形和平均值对点进行分组。只需使用我提供的代码,但按cellnumber进行聚合,cellnumber表示光栅单元的唯一ID。塔普利(pts@data$MyValue,pts@data$cells,FUN=平均值)
require(raster)
require(sp)

# Create example data
r <- raster(ncol=500, nrow=500)
  r[] <- runif(ncell(r))
    pts <- sampleRandom(r, 100, sp=TRUE)  

# Add a grouping value to points 
pts@data <- data.frame(ID=rownames(pts@data), group=c( rep(1,25),rep(2,25),
                       rep(3,25),rep(4,25)) )          

# Extract raster values and add to @data slot dataframe. Note, the "cells" 
#   attribute indicates the cell index in the raster. 
pts@data <- data.frame(pts@data, extract(r, pts, cellnumbers=TRUE))
  head(pts@data)

# Use tapply to cal group means  
tapply(pts@data$layer, pts@data$group, FUN=mean)