subset_single function

Extract values from a SpatRaster, SpatVector or SpatExtent

Extract values from a SpatRaster, SpatVector or SpatExtent

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 signature 'SpatRaster,ANY,ANY,ANY' x[i, j, k] ## S4 method for signature 'SpatVector,numeric,numeric' x[i, j, drop=FALSE] ## S4 method for signature 'SpatVector,SpatVector,missing' x[i, j] ## S4 method for signature 'SpatExtent,numeric,missing' x[i, j]

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

Returns

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

See Also

extract, subset, $, [[

Examples

### 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]
  • Maintainer: Robert J. Hijmans
  • License: GPL (>= 3)
  • Last published: 2025-02-26