I want to apply the case_when function over those columns who are factors and have only 2 levels and whose names do not contain "kiwi".
This does not work. I can get what I want with other longer approaches but I was wondering if there is any way to do this is an efficient way.
librare(dplyr)
library(stringr)
df <- data.frame(orange = factor(c(1,1,0,1,0)),
apple = factor(c("x", "x", "y", "y", "x")),
peach = 1:5,
kiwi = factor(c("a", "a", "b", "b", "c")))
df2 <- df %>%
mutate_if((~ is.factor(.) & nlevels(.) == 2 & str_detect(names(.), "kiwi", negate = TRUE)),
~ dplyr::case_when(.x == 0, "No",
.x == 1 ~ "Yes",
TRUE ~ .x))
From Operate on a selection of variables, it said
Scoped verbs (
_if
,_at
,_all
) have been superseded by the use ofpick()
oracross()
in an existing verb. Seevignette("colwise")
for details.
With across()
, you can use the <tidy-select> syntax to keep or drop columns:
where(is.factor)
where(~ nlevels(.x) == 2)
!contains("kiwi")
df %>%
mutate(across(where(~ is.factor(.x) & nlevels(.x) == 2) & !contains("kiwi"),
~ case_when(.x == 0 ~ "No",
.x == 1 ~ "Yes",
TRUE ~ .x)))