I'd like to identify variables with starts_with() and then perform a case_when mutation.
For example, let's say I want to do the equivalent of:
mtcars$mpg[mtcars$mpg == 21.0] <- 5; mtcars
My attempt:
mtcars %>%
mutate_at(
vars(starts_with("mpg")),
funs(. = case_when(
. == 21.0 ~ 5,
TRUE ~ .
))
)
What am I doing wrong? The dplyr documentation doesn't seem to have many examples of mutate_at/mutate_each (this thread seems to have the same complaint), so I have a hard time with these functions. Maybe I am not looking in the right place?
I'm aware of this thread but wasn't able to find a solution in there.
Thanks!