Learn R Programming

raster (version 1.8-3)

cellStats: Cell statistics

Description

Compute statistics for the cells of each layer of a Raster object. In the raster package, functions such as max, min, and mean, when used with Raster objects as argument, return a new Raster object (with a value computed for each cell). In contrast, cellStats returns a single value, computed from the all the values of a layer.

Usage

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

Arguments

x
A Raster* object
stat
The function to be applied
...
Additional arguments

Value

  • Numeric.

Details

cellStats will fail (gracefully) for very large Raster objects except for a number of known functions: sum, mean, min, max, sd, 'countNA'. 'countNA' must be supplied as a character value (with quotes), the other known functions may be supplied with or without quotes. For other functions you could perhaps use a sample of the RasterLayer that can be held in memory (see sampleRandom and sampleRegular ) For large files, and when fun='sd', this function returns the standard deviation of the population ( sqrt((1/N) * sum(x^2) - mean(x)^2)), not that of a sample which is normally returned by sd)

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)
# multi-layer object
cellStats(brick(r,r), mean)

Run the code above in your browser using DataLab