
Last chance! 50% off unlimited learning
Sale ends in
Select a subset of layers from a SpatRaster or select a subset of records (row) and/or variables (columns) from a SpatVector.
# S4 method for SpatRaster
subset(x, subset, negate=FALSE, NSE=FALSE, filename="", overwrite=FALSE, ...)# S4 method for SpatVector
subset(x, subset, select, drop=FALSE, NSE=FALSE)
if x
is a SpatRaster
: SpatRaster
if x
is a SpatVector
: SpatVector or, if drop=TRUE
, a data.frame
.
SpatRaster or SpatVector
if x
is a SpatRaster
: integer or character to select layers
if x
is a SpatVector
: logical expression indicating the rows to keep (missing values are taken as FALSE)
expression, indicating columns to select
logical. If TRUE
all layers that are not in the subset are selected
logical. If TRUE
, non-standard evaluation (the use of unquoted variable names) is allowed. Set this to FALSE
when calling subset
from a function
logical. If TRUE
, the geometries will be dropped, and a data.frame is returned
character. Output filename
logical. If TRUE
, filename
is overwritten
additional arguments for writing files as in writeRaster
### SpatRaster
s <- rast(system.file("ex/logo.tif", package="terra"))
subset(s, 2:3)
subset(s, c(3,2,3,1))
#equivalent to
s[[ c(3,2,3,1) ]]
s[[c("red", "green")]]
s$red
# expression based (partial) matching of names with single brackets
s["re"]
s["^re"]
# not with double brackets
# s[["re"]]
### SpatVector
v <- vect(system.file("ex/lux.shp", package="terra"))
subset(v, v$NAME_1 == "Diekirch", c("NAME_1", "NAME_2"))
subset(v, NAME_1 == "Diekirch", c(NAME_1, NAME_2), NSE=TRUE)
# or like this
v[2:3,]
v[1:2, 2:3]
v[1:2, c("NAME_1", "NAME_2")]
Run the code above in your browser using DataLab