Skip to contents

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

Usage

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

# S3 method for 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.

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)
#> class       : SpatRaster 
#> dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
#> resolution  : 10, 10  (x, y)
#> extent      : 100, 200, 100, 200  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) 
#> source(s)   : memory
#> name        : lyr.1 
#> min value   :     1 
#> max value   :   100 

# By name
spatrast %>% select(lyr.1:lyr.4)
#> class       : SpatRaster 
#> dimensions  : 10, 10, 4  (nrow, ncol, nlyr)
#> resolution  : 10, 10  (x, y)
#> extent      : 100, 200, 100, 200  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) 
#> source(s)   : memory
#> names       : lyr.1, lyr.2, lyr.3, lyr.4 
#> min values  :     1,   101,   201,   301 
#> max values  :   100,   200,   300,   400 

# Rename
spatrast %>% select(a = lyr.1, c = lyr.6)
#> class       : SpatRaster 
#> dimensions  : 10, 10, 2  (nrow, ncol, nlyr)
#> resolution  : 10, 10  (x, y)
#> extent      : 100, 200, 100, 200  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) 
#> source(s)   : memory
#> names       :   a,   c 
#> min values  :   1, 501 
#> max values  : 100, 600 

# SpatVector method

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

v <- vect(f)

v
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 3  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  source      : cyl.gpkg
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2  cpro   name
#>  type        : <chr> <chr>  <chr>
#>  values      : ES-AV    05  Avila
#>                ES-BU    09 Burgos
#>                ES-LE    24   Leon

v %>% select(1, 3)
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 2  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  source      : cyl.gpkg
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2   name
#>  type        : <chr>  <chr>
#>  values      : ES-AV  Avila
#>                ES-BU Burgos
#>                ES-LE   Leon

v %>% select(iso2, name2 = cpro)
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 2  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  source      : cyl.gpkg
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2 name2
#>  type        : <chr> <chr>
#>  values      : ES-AV    05
#>                ES-BU    09
#>                ES-LE    24