1

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?

Alexander
  • 59
  • 1
  • 8

2 Answers2

2

Summary functions (min, max, mean, etc.) in the raster package return a new raster object where each cell is a new computed value. raster::cellStats() is needed to return a single summary of all cell values in a layer. For example, to obtain the mean of layer means, you could use something like the following:

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
Julia Wilkerson
  • 581
  • 4
  • 4
1

How about

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

This applies summary to a vector containing all the values of your RasterLayer (and not to the RasterLayer itself). This should give you the following info (hence including the mean):

Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
ztl
  • 2,512
  • 1
  • 26
  • 40
  • Thanks ztl! Not sure why it wouldn;t display the mean with just the summary- but this works and I will be sure to use it for now on! – Alexander Mar 07 '17 at 13:36