Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

cellStats

Statistics across cells


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. Also see layerStats

Usage

## S4 method for signature 'RasterLayer'
cellStats(x, stat='mean', na.rm=TRUE, asSample=TRUE, ...)

## S4 method for signature 'RasterStackBrick'
cellStats(x, stat='mean', na.rm=TRUE, asSample=TRUE, ...)

Arguments

x

Raster* object

stat

The function to be applied. See Details

na.rm

Logical. Should NA values be removed?

asSample

Logical. Only relevant for stat=sd in which case, if TRUE, the standard deviation for a sample (denominator is n-1) is computed, rather than for the population (denominator is n)

...

Additional arguments

Details

cellStats will fail (gracefully) for very large Raster* objects except for a number of known functions: sum, mean, min, max, sd, 'skew' and 'rms'. 'skew' (skewness) and 'rms' (Root Mean Square) 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 sampleRegular )

Value

Numeric

See Also

Examples

r <- raster(nrow=18, ncol=36)
values(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)

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.