Last chance! 50% off unlimited learning
Sale ends in
Extract values from a SpatRaster; a subset of records (row) and/or variables (columns) from a SpatVector; or a number from a SpatExtent.
You can use indices (row, column, layer or cell numbers) to extract. You can also use other Spat* objects.
# S4 method for SpatRaster,ANY,ANY,ANY
[(x, i, j, k)# S4 method for SpatVector,numeric,numeric
[(x, i, j, drop=FALSE)
# S4 method for SpatVector,SpatVector,missing
[(x, i, j)
# S4 method for SpatExtent,numeric,missing
[(x, i, j)
numeric if x
is a SpatExtent.
Same as x
if drop=FALSE
. Otherwise a data.frame
SpatRaster, SpatVector or SpatExtent
if x
is a SpatRaster
: numeric, logical or missing to select rows or, if j
is missing, to select cells numbers.
if x
is a SpatVector
: numeric or missing to select rows.
if i
is another SpatVector: get a new SpatVector with the geometries that intersect.
if x
is a SpatExtent
: integer between 1 and 4.
numeric, logical, or missing to select columns
numeric, character, or missing to select layers
logical. If FALSE
an object of the same class as x
is returned
linkextract, link{subset}, link{$}, link{[[},
### SpatRaster
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
r[3638]
rowColFromCell(r, 2638)
r[39, 28]
x <- r[39:40, 28:29, drop=FALSE]
as.matrix(x, wide=TRUE)
### SpatVector
v <- vect(system.file("ex/lux.shp", package="terra"))
v[2:3,]
v[1:2, 2:3]
v[1:2, 2:3, drop=TRUE]
Run the code above in your browser using DataLab