Learn R Programming

tidyterra (version 0.1.0)

relocate: Change layer/attribute order

Description

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.

Usage

# S3 method for SpatRaster
relocate(.data, ..., .before = NULL, .after = NULL)

# S3 method for SpatVector relocate(.data, ..., .before = NULL, .after = NULL)

Arguments

.data

A SpatRaster created with terra::rast() or a SpatVector created with terra::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.

Value

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

terra equivalent

terra::subset(data, c("name_layer", "name_other_layer"))

Methods

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

SpatRaster

Relocate layers of a SpatRaster.

SpatVector

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.

See Also

dplyr::relocate()

Other dplyr methods: filter(), mutate(), pull(), rename(), select(), slice()

Examples

Run this code
# NOT RUN {
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