Skip to contents

rename() changes the names of individual layers/attributes using new_name = old_name syntax; rename_with() renames layers/attributes using a function.

Usage

# S3 method for class 'SpatRaster'
rename(.data, ...)

# S3 method for class 'SpatRaster'
rename_with(.data, .fn, .cols = everything(), ...)

# S3 method for class 'SpatVector'
rename(.data, ...)

# S3 method for class 'SpatVector'
rename_with(.data, .fn, .cols = everything(), ...)

Arguments

.data

A SpatRaster created with terra::rast() or a SpatVector created with terra::vect().

...

Depending on the function:

  • For rename.Spat*(): <tidy-select>. Use new_name = old_name to rename selected variables.

  • For rename_with(): additional arguments passed onto .fn.

.fn

A function used to transform the selected .cols. Should return a character vector the same length as the input.

.cols

<tidy-select> Columns to rename; defaults to all columns.

Value

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

terra equivalent

names(Spat*) <- c("a", "b", "c")

Methods

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

SpatRaster

Rename layers of a SpatRaster.

SpatVector

The result is a SpatVector with the renamed attributes on the function call.

Examples


library(terra)
f <- system.file("extdata/cyl_tile.tif", package = "tidyterra")
spatrast <- rast(f) %>% mutate(aa = 1, bb = 2, cc = 3)

spatrast
#> 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_1, cyl_tile_2, cyl_tile_3, aa, bb, cc 
#> min values  :         35,         35,         35,  1,  2,  3 
#> max values  :        253,        251,        250,  1,  2,  3 

spatrast %>% rename(
  this_first = cyl_tile_1,
  this_second = cyl_tile_2
)
#> 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       : this_first, this_second, cyl_tile_3, aa, bb, cc 
#> min values  :         35,          35,         35,  1,  2,  3 
#> max values  :        253,         251,        250,  1,  2,  3 

spatrast %>% rename_with(
  toupper,
  .cols = starts_with("c")
)
#> 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_1, CYL_TILE_2, CYL_TILE_3, aa, bb, CC 
#> min values  :         35,         35,         35,  1,  2,  3 
#> max values  :        253,        251,        250,  1,  2,  3