Learn R Programming

terra (version 1.7-55)

subset_single: Extract values from a SpatRaster, SpatVector or SpatExtent

Description

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.

Usage

# 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)

Value

numeric if x is a SpatExtent. Same as x if drop=FALSE. Otherwise a data.frame

Arguments

x

SpatRaster, SpatVector or SpatExtent

i

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.

j

numeric, logical, or missing to select columns

k

numeric, character, or missing to select layers

drop

logical. If FALSE an object of the same class as x is returned

See Also

extract, subset, $, [[

Examples

Run this code
### 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