Learn R Programming

tidyterra (version 1.3.0)

replace_na.Spat: Replace NAs with specified values

Description

Replace NAs values on layers/attributes with specified values

Usage

# S3 method for SpatRaster
replace_na(data, replace = list(), ...)

# S3 method for SpatVector replace_na(data, replace, ...)

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().

replace

A named list of values, with one value for each layer/attribute that has missing values to be replaced. Each value in replace will be cast to the type of the column in data where it is used as a replacement.

...

Additional arguments for methods. Currently unused.

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

Use r[is.na(r)] <- <replacement>.

See Also

tidyr::replace_na().

Other tidyr verbs for handling missing values: complete.SpatVector(), drop_na.Spat, expand.SpatVector(), fill.SpatVector()

Examples

Run this code

library(terra)

f <- system.file("extdata/cyl_temp.tif", package = "tidyterra")
r <- rast(f)

r |> plot()

r |>
  replace_na(list(tavg_04 = 6, tavg_06 = 20)) |>
  plot()

Run the code above in your browser using DataLab