This is a translation of the SQL command NULL_IF
. It is useful
if you want to convert an annoying value to NA
.
na_if(x, y)
x | Vector to modify |
---|---|
y | Value to replace with NA |
A modified version of x
that replaces any values that
are equal to y
with NA.
coalesce()
to replace missing values with a specified
value.
na_if(1:5, 5:1)#> [1] 1 2 NA 4 5x <- c(1, -1, 0, 10) 100 / x#> [1] 100 -100 Inf 10100 / na_if(x, 0)#> [1] 100 -100 NA 10y <- c("abc", "def", "", "ghi") na_if(y, "")#> [1] "abc" "def" NA "ghi"