Search code examples
rdplyrr-factorforcats

factor many mutated variables in one fell swoop


If I have 20 variables I've already recoded, why can't I pipe the mutate() into as_factor()? I don't get an error but it just doesn't do any leveling.

I can do it one by one df$Ethnicity %<>% as_factor()

but I can't do:

df %<>% 
     mutate(
      Gender = case_when(
      Q4 == 1 ~ "Male", 
      Q4 == 2 ~ "Female",
      TRUE ~ as.character("Other")),

      Education = case_when(
        Education_n %in% c(1:4) ~ "Low", 
        Education_n %in% c(5:8) ~ "Medium", 
        Education_n %in% c(9:11) ~ "High", 
        TRUE ~ NA_character_)) %>% 
as_factor()

I've tried it as_factor(.) too, to no avail. Do I really have to manually wrap every case_when in factor()? I have twenty variables, I'd like to avoid a solution that requires writing each one out into a purr:: function or repeating wrapping factor() twenty times.


Solution

  • If you don't want to convert each column to factor you can use across to turn range of columns to factor.

    df %<>% 
       mutate(
         Gender = case_when(
                Q4 == 1 ~ "Male", 
                Q4 == 2 ~ "Female",
                TRUE ~ as.character("Other")),
    
         Education = case_when(
            Education_n %in% c(1:4) ~ "Low", 
            Education_n %in% c(5:8) ~ "Medium", 
            Education_n %in% c(9:11) ~ "High", 
            TRUE ~ NA_character_)) %>%
        mutate(across(Gender:Education, as_factor))
    

    If you have an older version of dplyr use mutate_at :

    mutate_at(vars(Gender:Education), as_factor)