Learn R Programming

terra (version 0.8-6)

local: Local statistics

Description

Compute cell (pixel) level "local" statistics across layers or between layers (parallel summary).

The following summary methods are available for SpatRaster: any, all, max, min, mean, median, prod, range, stdev, sum, which.min, which.max

To compute statistics that are not included here see app to summarize across layers.

See modal to compute the mode.

Because generic functions are used, the method applied is chosen based on the first argument: "x". This means that if r is a SpatRaster, mean(r, 5) will work, but mean(5, r) will not work.

The mean method has an argument "trim" that is ignored.

The stdev method returns the population standard deviation, computed as:

f <- function(x) sqrt(sum((x-mean(x))^2) / length(x))

This is different than the sample standard deviation returned by sd (which uses n-1 as denominator). Function f above is equivalent to function g below

g <- function(x) sqrt(sum((x-mean(x))^2) / length(x))

Usage

# S4 method for SpatRaster
min(x, ..., na.rm=FALSE)

# S4 method for SpatRaster max(x, ..., na.rm=FALSE)

# S4 method for SpatRaster range(x, ..., na.rm=FALSE)

# S4 method for SpatRaster mean(x, ..., trim=NA, na.rm=FALSE)

# S4 method for SpatRaster median(x, na.rm=FALSE, ...)

# S4 method for SpatRaster stdev(x, ..., na.rm=FALSE)

# S4 method for SpatRaster which.min(x)

# S4 method for SpatRaster which.max(x)

Arguments

x

SpatRaster

...

additional SpatRaster objects or numeric values

trim

ignored

na.rm

logical. If TRUE, NA values are ignored. If FALSE, NA is returned if x has any NA values

Value

SpatRaster

See Also

Math-methods, modal

Examples

Run this code
# NOT RUN {
set.seed(0)
r <- rast(nrow=10, ncol=10, nlyr=3)
values(r) <- runif(size(r))

x <- mean(r)
# note how this returns one layer
x <- sum(c(r, r[[2]], 5))

# and this returns three layers
y <- sum(r, r[[2]], 5)

max(r)
max(r, 0.5)

y <- stdev(r)
# not the same as 
yy <- app(r, sd)

z <- stdev(r, r*2)
# }

Run the code above in your browser using DataLab