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

rapp

Range-Apply


Description

Apply a function to a cell-varying range of the layers of a SpatRaster. The range is specified for each cell separately by another SpatRaster with two layers. For either first or last you can use a constant instead.

See selectRange to create a new SpatRaster by extracting one or more values starting at a cell-varying layer.

See app or Summary-methods if you want to apply a function to all layers (perhaps after making a subset) of a SpatRaster.

Usage

## S4 method for signature 'SpatRaster'
rapp(x, first, last, fun, ..., allyrs=FALSE, fill=NA, 
        clamp=FALSE, filename="", overwrite=FALSE, wopt=list())

Arguments

x

SpatRaster

first

SpatRaster or positive integer between 1 and nlyr(x), indicating the first layer in the range of layers to be considered

last

SpatRaster or positive integer between 1 and nlyr(x), indicating the last layer in the range to be considered

fun

function to be applied

...

additional arguments passed to fun

allyrs

logical. Should the values of all layers be passed to fun. The values outside of the range are set to fill

fill

numeric. The fill value for the the values outside of the range, for when allyrs=TRUE

clamp

logical. If FALSE and the specified range is outside 1:nlyr(x) all cells are considered NA. Otherwise, the invalid part of the range is ignored

filename

character. Output filename

overwrite

logical. If TRUE, filename is overwritten

wopt

list with named options for writing files as in writeRaster

Value

SpatRaster

See Also

Examples

r <- rast(ncol=9, nrow=9)
values(r) <- 1:ncell(r)
s <- c(r, r, r, r, r, r)
s <- s * 1:6
s[1:2] <- NA
start <- end <- rast(r)
start[] <- 1:3
end[]   <- 4:6
a <- rapp(s, start, end, fun="mean")
b <- rapp(s, start, 2, fun="mean")

# cumsum from start to nlyr(x). return all layers
r <- rapp(s, start, nlyr(s), cumsum, allyrs=TRUE, fill=0)
# return only the final value
rr <- rapp(s, start, nlyr(s), function(i) max(cumsum(i)))

terra

Spatial Data Analysis

v1.2-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Roger Bivand [ctb] (<https://orcid.org/0000-0003-2392-6140>), Karl Forner [ctb], Jeroen Ooms [ctb] (<https://orcid.org/0000-0002-4035-0289>), Edzer Pebesma [ctb] (<https://orcid.org/0000-0001-8049-7069>)
Initial release
2021-05-12

We don't support your browser anymore

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