Last chance! 50% off unlimited learning
Sale ends in
raster
package, functions such as max, min, and mean,
when used with a Raster objects as argument, return a new RasterLayer (with a value computed for each cell). In contrast, cellStats returns a single value,
computed from the all the values of a layer.cellStats(x, stat='mean', ...)
rasterOptions
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)
quantile
, minValue
, maxValue
, setMinMax
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)
# not quite the same
cellStats(r, 'sd')
cellStats(r, sd)
Run the code above in your browser using DataLab