subset.Rd
Extract a set of layers from a RasterStack or RasterBrick object.
# S4 method for class 'Raster'
subset(x, subset, drop=TRUE, filename='', ...)
# S4 method for class 'RasterStack'
subset(x, subset, drop=TRUE, filename='', ...)
RasterBrick or RasterStack object
integer or character. Should indicate the layers (represented as integer or by their name)
If TRUE
, a selection of a single layer will be returned as a RasterLayer
character. Output filename (optional)
additional arguments as for writeRaster
Raster* object
s <- stack(system.file("external/rlogo.grd", package="raster"))
sel <- subset(s, 2:3)
# Note that this is equivalent to
sel2 <- s[[2:3]]
# and in this particular case:
sel3 <- dropLayer(s, 1)
nlayers(s)
#> [1] 3
nlayers(sel)
#> [1] 2
# effect of 'drop=FALSE' when selecting a single layer
sel <- subset(s, 2)
class(sel)
#> [1] "RasterLayer"
#> attr(,"package")
#> [1] "raster"
sel <- subset(s, 2, drop=FALSE)
class(sel)
#> [1] "RasterStack"
#> attr(,"package")
#> [1] "raster"