Learn R Programming

raster (version 1.5-8)

stackApply: Apply a function on subsets of a RasterStack or RasterBrick

Description

Apply a function on subsets of a RasterStack or RasterBrick. The layers to be combined are indicated with the vector indices.

Usage

stackApply(x, indices, fun, filename='', na.rm=TRUE, ...)

Arguments

x
A Raster* object
indices
A vector of length nlayers(x) with values between 1 and nlayers(x)
fun
A function that returns a single value, e.g. mean or min, and that takes an 'na.rm' argument
na.rm
Logical. If TRUE, NA cells are removed from calculations
filename
Character. Optional output filename
...
Additional arguments. See below, under Methods

Value

  • A new Raster* object, and in some cases the side effect of a new file on disk.

Methods

The following additional arguments can be passed, to replace default values for this function rll{ format Character. Output file type. See writeRaster datatype Character. Output data type. See dataType overwrite Logical. If TRUE, "filename" will be overwritten if it exists progress Character. "text", "window", or "" (the default, no progress bar) }

See Also

calc

Examples

Run this code
r <- raster(ncol=10, nrow=10)
r[]=1:ncell(r)
s <- brick(r,r,r,r,r,r)
s <- s * 1:6
b <- stackApply(s, indices=c(1,1,2,2,3,3), fun=sum)
b

Run the code above in your browser using DataLab