rtidyversecase-whenacross

case_when using contains instead of declaring each variable to evaluate


I would like to try to avoid stating which variables to assess by instead relying on the suffix _num. mutating a variable conditional on a variable name is possible, but this code needs updating because it should now use across.

df %<>% mutate(`Manipulation1` = case_when(
A1_num > 2 ~ "support",
B1_num > 2 ~ "support",
C1_num > 2 ~ "support",
A4_num > 2 ~ "support",
B4_num > 2 ~ "support",
C4_num > 2 ~ "support",
same 6 variables == 2 ~ "neither", 
same 6 variables < 2 ~ "reject",
TRUE~ NA_character_))

This answer using contains doesn't work for this scenario because it is evaluating responses in a column, not the variable name.


Solution

  • We could wrap with if_any

    library(dplyr)
    library(stringr)
    nm1 <- str_c(LETTERS[1:3], rep(c(1, 4), each = 3), "_num")
    df <- df %>%
          mutate(s1 = if_any(all_of(nm1), ~ . > 2),
                 n1 = if_any(all_of(nm1), ~ . == 2),
                 r1 = if_any(all_of(nm1), ~ . < 2),
                 Manipulation1 = case_when(s1 ~ "support",
                                          n1 ~ "neither",
                                          r1 ~ "reject",
                                TRUE ~ NA_character_),
                         s1 = NULL, n1 = NULL, r1 = NULL)