I would like to list all the occurrences in a tibble that I want to convert to missing using the na_if
function un the dplyr package but I dont seem to get it right. Any leads?
library(dplyr)
set.seed(123)
df <- tibble(
a1 = c("one", "three", "97", "twenty", "98"),
a2 = c("R", "Python", "99", "Java", "97"),
a3 = c("statistics", "Data", "Programming", "99", "Science"),
a4 = floor(rnorm(5, 80, 2))
)
#--- The long route
df1 <- df %>%
mutate(across(where(is.character), ~na_if(., "97")),
across(where(is.character), ~na_if(., "98")),
across(where(is.character), ~na_if(., "99")))
#---- Trial
df2 <- df %>%
mutate(across(where(is.character),
~na_if(., c("97", "98", "99"))))