I want to replace values for multiple columns to NA based on the values in the other columns.
I end up with the following code, but I can't figure out how to refer to the original value from the column (if it shouldn't be replaced). In the example below, I want to replace values of displ, cty, why to NA if cyl equal 4.
suppressMessages(library(tidyverse))
mpg %>%
mutate(across(.cols = c(displ, cty, hwy),
.fns = case_when(cyl == 4 ~ as.numeric(NA),
TRUE ~ .x)))
#> Error: Problem with `mutate()` input `..1`.
#> x object '.x' not found
#> i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.
Curiously, when I run the same code on a different machine, the error is different
suppressMessages(library(tidyverse))
mpg %>%
mutate(across(.cols = c(displ, cty, hwy),
.fns = case_when(cyl == 4 ~ as.numeric(NA),
TRUE ~ .x)))
#> Error: Problem with `mutate()` input `..1`.
#> x object 'cyl' not found
#> i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.