Use relocate()
to change layer/attribute positions, using the same syntax
as select()
to make it easy to move blocks of layers/attributes at once.
# S3 method for SpatRaster
relocate(.data, ..., .before = NULL, .after = NULL)# S3 method for SpatVector
relocate(.data, ..., .before = NULL, .after = NULL)
A Spat* object of the same class than .data
. See Methods.
A SpatRaster created with terra::rast()
or a SpatVector
created with terra::vect()
.
tidy-select
layers/attributes to move.
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::subset(data, c("name_layer", "name_other_layer"))
Implementation of the generic dplyr::relocate()
function.
Relocate layers of a SpatRaster.
This method relies on the implementation of dplyr::relocate()
method on the
sf package. The result is a SpatVector with the attributes on a different
order.
Other dplyr methods:
filter.Spat
,
mutate.Spat
,
pull.Spat
,
rename.Spat
,
select.Spat
,
slice.Spat
library(terra)
f <- system.file("extdata/cyl_tile.tif", package = "tidyterra")
spatrast <- rast(f) %>% mutate(aa = 1, bb = 2, cc = 3)
names(spatrast)
spatrast %>%
relocate(bb, .before = cyl_tile_3) %>%
relocate(cyl_tile_1, .after = last_col())
Run the code above in your browser using DataLab