replace_na.Spat: Replace 'NA's with specified values

replace_na.SpatR Documentation

Replace NAs with specified values

Description

Replace NAs values on layers/attributes with specified values

Usage

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

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

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 that it being used as a replacement in.

...

Additional arguments for methods. Currently unused.

Value

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

terra equivalent

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

See Also

tidyr::replace_na()

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

Other tidyr methods: drop_na.Spat, fill.SpatVector(), pivot_longer.SpatVector(), pivot_wider.SpatVector()

Examples


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


tidyterra documentation built on March 11, 2026, 9:08 a.m.