na_if | R Documentation |
This is a translation of the SQL command NULLIF
. It is useful if you want to convert an annoying value to NA
.
na_if(x, y)
x |
The vector to modify. |
y |
The value to replace with |
A modified version of x
that replaces any values that are equal to y
with NA
.
coalesce()
to replace missing values within subsequent vector
(s) of value(s). replace_na()
to replace NA
with
a value.
replace_na()
to replace NA
with a value.
recode()
to more generally replace values.
na_if(1:5, 5:1)
x <- c(1, -1, 0, 10)
100 / x
100 / na_if(x, 0)
y <- c("abc", "def", "", "ghi")
na_if(y, "")
# na_if() is particularly useful inside mutate(),
# and is meant for use with vectors rather than entire data.frames
mtcars %>%
mutate(cyl = na_if(cyl, 6))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.