select.Spat {tidyterra}R Documentation

Subset layers/attributes of ⁠Spat*⁠ objects

Description

Select (and optionally rename) attributes/layers in ⁠Spat*⁠ objects, using a concise mini-language. See Methods.

Usage

## S3 method for class 'SpatRaster'
select(.data, ...)

## S3 method for class 'SpatVector'
select(.data, ...)

Arguments

.data

A SpatRaster created with terra::rast() or a SpatVector created with terra::vect().

...

<tidy-select> One or more unquoted expressions separated by commas. Layer/attribute names can be used as if they were positions in the ⁠Spat*⁠ object, so expressions like x:y can be used to select a range of layers/attributes.

Value

A ⁠Spat*⁠ object of the same class than .data. See Methods.

terra equivalent

terra::subset()

Methods

Implementation of the generic dplyr::select() function.

SpatRaster

Select (and rename) layers of a SpatRaster. The result is a SpatRaster with the same extent, resolution and crs than .data. Only the number (and possibly the name) of layers is modified.

SpatVector

The result is a SpatVector with the selected (and possibly renamed) attributes on the function call.

See Also

dplyr::select(), terra::subset()

Other single table verbs: arrange.SpatVector(), filter.Spat, mutate.Spat, rename.Spat, slice.Spat, summarise.SpatVector()

Other dplyr verbs that operate on columns: glimpse.Spat, mutate.Spat, pull.Spat, relocate.Spat, rename.Spat

Other dplyr methods: arrange.SpatVector(), bind_cols.SpatVector, bind_rows.SpatVector, count.SpatVector(), distinct.SpatVector(), filter-joins.SpatVector, filter.Spat, glimpse.Spat, group-by.SpatVector, mutate-joins.SpatVector, mutate.Spat, pull.Spat, relocate.Spat, rename.Spat, rowwise.SpatVector(), slice.Spat, summarise.SpatVector()

Examples


library(terra)

# SpatRaster method

spatrast <- rast(
  crs = "EPSG:3857",
  nrows = 10,
  ncols = 10,
  extent = c(100, 200, 100, 200),
  nlyr = 6,
  vals = seq_len(10 * 10 * 6)
)

spatrast %>% select(1)

# By name
spatrast %>% select(lyr.1:lyr.4)

# Rename
spatrast %>% select(a = lyr.1, c = lyr.6)

# SpatVector method

f <- system.file("extdata/cyl.gpkg", package = "tidyterra")

v <- vect(f)

v

v %>% select(1, 3)

v %>% select(iso2, name2 = cpro)

[Package tidyterra version 0.6.1 Index]