Select (and optionally rename) attributes/layers in Spat*
objects, using a
concise mini-language. See Methods.
Arguments
- .data
A
SpatRaster
created withterra::rast()
or aSpatVector
created withterra::vect()
.- ...
<
tidy-select
> One or more unquoted expressions separated by commas. Layer/attribute names can be used as if they were positions in theSpat*
object, so expressions likex:y
can be used to select a range of layers/attributes.
terra equivalent
Methods
Implementation of the generic dplyr::select()
function.
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)
#> 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