Learn R Programming

tidyterra (version 0.7.2)

mutate.Spat: Create, modify, and delete cell values/layers/attributes of Spat* objects

Description

mutate() adds new layers/attributes and preserves existing ones on a Spat* object. transmute() adds new layers/attributes and drops existing ones. New variables overwrite existing variables of the same name. Variables can be removed by setting their value to NULL.

Usage

# S3 method for SpatRaster
mutate(.data, ...)

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

# S3 method for SpatRaster transmute(.data, ...)

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

Value

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

Arguments

.data

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

...

<data-masking> Name-value pairs. The name gives the name of the layer/attribute in the output.

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

Some terra methods for modifying cell values: terra::ifel(), terra::classify(), terra::clamp(), terra::app(), terra::lapp(), terra::tapp()

Methods

Implementation of the generic dplyr::mutate(), dplyr::transmute() functions.

SpatRaster

Add new layers and preserves existing ones. The result is a SpatRaster with the same extent, resolution and crs than .data. Only the values (and possibly the number) of layers is modified.

transmute() would keep only the layers created with ....

SpatVector

The result is a SpatVector with the modified (and possibly renamed) attributes on the function call.

transmute() would keep only the attributes created with ....

See Also

dplyr::mutate(), dplyr::transmute() methods.

terra provides several ways to modify Spat* objects:

Other single table verbs: arrange.SpatVector(), filter.Spat, rename.Spat, select.Spat, slice.Spat, summarise.SpatVector()

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

Other dplyr methods: arrange.SpatVector(), bind_cols.SpatVector, bind_rows.SpatVector, count.SpatVector(), distinct.SpatVector(), filter-joins.SpatVector, filter.Spat, glimpse.Spat, group-by.SpatVector, mutate-joins.SpatVector, pull.Spat, relocate.Spat, rename.Spat, rowwise.SpatVector(), select.Spat, slice.Spat, summarise.SpatVector()

Examples

Run this code

library(terra)

# SpatRaster method
f <- system.file("extdata/cyl_temp.tif", package = "tidyterra")
spatrast <- rast(f)

mod <- spatrast %>%
  mutate(exp_lyr1 = exp(tavg_04 / 10)) %>%
  select(tavg_04, exp_lyr1)

mod
plot(mod)

# SpatVector method
f <- system.file("extdata/cyl.gpkg", package = "tidyterra")
v <- vect(f)

v %>%
  mutate(cpro2 = paste0(cpro, "-CyL")) %>%
  select(cpro, cpro2)

Run the code above in your browser using DataLab