rapp | R Documentation |
Apply a function to a range of the layers of a SpatRaster that varies by cell. The range is specified for each cell with one or two SpatRasters (arguments first
and last
). For either first
or last
you can use a single number instead.
You cannot use single numbers for both first
and last
because in that case you could use app
or Summary-methods
, perhaps subset
ting the layers of a SpatRaster.
See selectRange
to create a new SpatRaster by extracting one or more values starting at a cell-varying layer.
## S4 method for signature 'SpatRaster'
rapp(x, first, last, fun, ..., allyrs=FALSE, fill=NA,
clamp=FALSE, circular=FALSE, filename="", overwrite=FALSE, wopt=list())
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 |
allyrs |
logical. If |
fill |
numeric. The fill value for the values outside of the range, for when |
clamp |
logical. If |
circular |
logical. If |
filename |
character. Output filename |
overwrite |
logical. If |
wopt |
list with named options for writing files as in |
SpatRaster
selectRange
, app
, Summary-methods
, lapp
, tapp
r <- rast(ncols=9, nrows=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)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.