如何重新编码变量中的一系列值并将其强制转换为一个因子?

How to recode a range of values in a variable and coerce it to a factor?

我正在寻找一种方法(干净地)将具有一系列值的变量重新编码为 R tidyverse 方式中的有序因子。这是我所拥有的示例:

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()

有没有办法在一行中完成此操作,这样我就不必在 ordered() 函数中重复标签?据我所知,我正在寻找类似 recode_factor() 的东西,它可以处理一系列值。

非常感谢!

使用带有标签的剪切

df$education_recoded <- cut(df$education, breaks = c(0,3,5), 
                            labels = c("Up to secondary school", 
                                       "University studies or higher"))
# compare the values
table(df$education_recoded, df$education)
#                               1  2  3  4  5
# Up to secondary school       21 24 24  0  0
# University studies or higher  0  0  0 15 16

或使用管道:

library(dplyr)  

df %>% 
  mutate(education_recoded = cut(education, breaks = c(0,3,5), 
                                 labels = c("Up to secondary school", 
                                            "University studies or higher")))