Use relocate()
to change layer/attribute positions, using the same syntax
as select.Spat
to make it easy to move blocks of
layers/attributes at once.
Arguments
- .data
A
SpatRaster
created withterra::rast()
or aSpatVector
created withterra::vect()
.- ...
<
tidy-select
> layers/attributes to move.- .before, .after
<
tidy-select
> Destination of layers/attributes selected by...
. Supplying neither will move layers/attributes to the left-hand side; specifying both is an error.
terra equivalent
terra::subset(data, c("name_layer", "name_other_layer"))
Methods
Implementation of the generic dplyr::relocate()
function.
See also
Other dplyr verbs that operate on columns:
glimpse.Spat
,
mutate.Spat
,
pull.Spat
,
rename.Spat
,
select.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
,
rename.Spat
,
rowwise.SpatVector()
,
select.Spat
,
slice.Spat
,
summarise.SpatVector()
Examples
library(terra)
f <- system.file("extdata/cyl_tile.tif", package = "tidyterra")
spatrast <- rast(f) %>% mutate(aa = 1, bb = 2, cc = 3)
names(spatrast)
#> [1] "cyl_tile_1" "cyl_tile_2" "cyl_tile_3" "aa" "bb"
#> [6] "cc"
spatrast %>%
relocate(bb, .before = cyl_tile_3) %>%
relocate(cyl_tile_1, .after = last_col())
#> class : SpatRaster
#> dimensions : 212, 261, 6 (nrow, ncol, nlyr)
#> resolution : 2445.985, 2445.985 (x, y)
#> extent : -812067, -173664.9, 4852834, 5371383 (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857)
#> source(s) : memory
#> names : cyl_tile_2, bb, cyl_tile_3, aa, cc, cyl_tile_1
#> min values : 35, 2, 35, 1, 3, 35
#> max values : 251, 2, 250, 1, 3, 253