I am looking for a way to (cleanly) recode a variable with a range of values into an ordered factor in R tidyverse way. This is an example of what I have:
set.seed(1505)
n <- 100
df <- data.frame(id=1:n,
education=sample(1:5, n, replace=TRUE))
df %>%
mutate(education_recoded=case_when(education %in% 1:3 ~ "Up to secondary school",
education %in% 4:5 ~ "University studies or higher"),
education_recoded=ordered(education_recoded, levels = c("Up to secondary school", "University studies or higher"))) %>%
as_tibble()
Is there a way to do this in one line, so that I don't have to repeat labels in the ordered() function? As far as I can tell, I am looking for something like recode_factor() that can work with a range of values.
Thanks a lot!