raster (version 1.1.7)

cellStats: Cell statistics

Description

Compute statistics for the cells of a single RasterLayer. In the raster package, functions such as max, min, and mean, when used with a RasterLayer objects as argument, return another RasterLayer. In contrasct, cellStats returns a single value, computed from the values of a single RasterLayer.

Usage

cellStats(raster, stat='mean', ...)

Arguments

raster
A RasterLayer
stat
The function to be applied. Either as character: 'mean', 'min', 'max', 'sum', or 'sd'; or a function (see Details)
...
Additional argument. See rasterOptions

Value

  • A numeric value.

Details

If stat is a function rather than a character string, zonal will fail (gracefully) for very large RasterLayers. In such cases you can use the character string function name if applicable. Otherwise you could use a sample of the RasterLayer that can be held in memory (see sampleRandom and sampleRegular ) stat='sd' returns slightly different values than stat=sd, because the former computes the standard deviation of the population (rather than of a sample) using this formula: sqrt((1/N) * sum(x^2) - mean(x)^2)

See Also

quantile, minValue, maxValue, setMinMax

Examples

Run this code
r <- raster(nrow=18, ncol=36)
r[] <- runif(ncell(r)) * 10
# works for large files
cellStats(r, 'mean')
# same, but does not work for very large files
cellStats(r, mean)

# not quite the same
cellStats(r, 'sd')
cellStats(r, sd)

Run the code above in your browser using DataCamp Workspace