rsapply accepts either a single RasterLayer or a multi-layer RasterStack/RasterBrick. At each pixel, fun will be called with a vector representing the values of each layer at that pixel. fun must return a vector of the same length at every pixel. If fun returns a vector with more than one element, the result will be returned as a RasterStack; otherwise, it will be returned as a RasterLayer.

rsapply(rast, fun, names = NULL)

Arguments

rast

A Raster*

fun

a function to apply to each pixel of the raster. The function will be called with a vector representing the pixel values of each raster in the stack, at a given (x,y).

names

the names of the values returned by fun

Value

a new RasterLayer or RasterStack containing the values returned by fun at each pixel.

Examples

# NOT RUN {
#
# Fit per-pixel GEV distributions, given a RasterStack of
# observed values.
rsapply(observed, function(pvals) {
 lmr <- lmom::samlmu(pvals, nmom = 5)
 ret <- try(lmom::pelgev(lmr), silent=FALSE)
}, names=c('location', 'scale', 'shape'))
# }
# NOT RUN { # Compute summary statistics over time-integrated observations rsapply(precip_6mo, function(pvals) { c(min(pvals), mean(pvals), max(pvals)) }, names=c('min', 'ave', 'max')) # }