Learn R Programming

tidyterra (version 1.3.0)

rename.Spat: Rename layers/attributes

Description

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 SpatRaster
rename(.data, ...)

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

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

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

Value

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

Arguments

.data

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

...

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

For rename_with.Spat*(): 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.

<a href="https://CRAN.R-project.org/package=terra"><span class="pkg">terra</span></a> equivalent

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

Methods

Implementation of the generic dplyr::rename() methods for Spat* objects.

SpatRaster

Renames layers of a SpatRaster.

SpatVector

The result is a SpatVector with the attributes renamed as specified in the function call.

See Also

dplyr::rename().

Other dplyr verbs that operate on columns: glimpse.Spat, mutate.Spat, pull.Spat, relocate.Spat, select.Spat

Examples

Run this code

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

spatrast

spatrast |> rename(
  this_first = cyl_tile_1,
  this_second = cyl_tile_2
)

spatrast |> rename_with(
  toupper,
  .cols = starts_with("c")
)

Run the code above in your browser using DataLab