Search code examples
rmeanrasterr-raster

Mean of RasterLayer will Not Compute (R Raster Package)


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?


Solution

  • 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.