This function takes an object and sets all non-missing values to zero, while leaving missing values unchanged.
Arguments
- x
A vector or other object for which
is.na()and subsetting with[]are defined (e.g., vector, data frame, SpatRaster).
Examples
# Example with a numeric vector
vec <- c(1, 2, NA, 4, NA, 5)
set_zero(vec)
#> [1] 0 0 NA 0 NA 0