I have a dataset where loads of columns have NA
characters because they depend on another column response. For example :
df <- data.frame(X_home = c("Yes", "Yes", "No"),
X_school = c("No", "Yes", "No"),
Y_home = c("A", "B", NA),
Y_school = c(NA, "A", NA))
If I use mutate()
, I can do :
df %>% mutate(Y_home = if_else(X_home == "No", "C", Y_home),
Y_school = if_else(X_school == "No", "C", Y_school))
to obtain what I want. But the problem is that I have many X_something
and Y_something
. So I'd like to make it with something like that :
df %>% mutate(across(starts_with("Y_"), ~ if_else(...))
Is it possible ?
Thanks a lot.