Skip to contents

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.

Usage

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

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

The result is a SpatVector with the attributes on a different order.

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