Mean of RasterLayer will Not Compute (R Raster Package)

时间:2017-03-02 23:32:03

标签: r mean raster r-raster

So, I have stacked multiple rasters (x1, x2, x3, x4, ...) and successfully computed a mean raster from all of those (xmaster). However, I then want the mean pixel value of that raster (xmaster). Normally I would display the summary statistics and call the mean value.... however no mean appears in the summary for 'xmaster'! I am not sure why- I am wondering if someone would kindly help me with a work around. Please see my below script:

lightstackFF<-list.files()
stacklights<-stack(lightstackFF)
xmaster<- mean(stacklights, na.rm=TRUE)
summary(xmaster)

"> summary(xmaster) layer Min. 11488 1st Qu. 18016 Median 20048 3rd Qu. 21968 Max. 28704 NA's 0"

As you guys can see, no mean value appears for the raster. Of course I can save the raster out and extract the mean in another software- but its very time consuming. Can anyone help me out as to why this is not showing the mean?

2 个答案:

答案 0 :(得分:1)

raster包中的汇总函数(min,max,mean等)返回一个新的栅格对象,其中每个单元格都是一个新的计算值。需要raster::cellStats()才能返回图层中所有单元格值的单个摘要。例如,要获得图层均值,您可以使用以下内容:

r <- raster(nrow=18, ncol=36)
r[] <- runif(ncell(r)) * 10
rs <- stack(r,r,r)
layermeans <- cellStats(rs, stat='mean', na.rm=TRUE)
u <- mean(layermeans)
> layermeans
 layer.1  layer.2  layer.3 
5.028814 5.028814 5.028814 
> u
[1] 5.028814

答案 1 :(得分:0)

怎么样

summary(xmaster[])    # Please note the []
# which is equivalent to:
summary(values(xmaster))
# or even
summary(getValues(xmaster))

这会将summary应用于包含RasterLayer的所有值的向量(而不是RasterLayer 本身) 。这应该给你以下信息(因此包括平均值):

Min. 1st Qu.  Median    Mean 3rd Qu.    Max.