如何从一个级别移动到另一个数据

问题描述 投票:0回答:1

这是我的数据

levels(ab$age)    
# [1] "18-25"        "26-30"       "31-35"    "36-40"    "41-45"    "46-50"    "51-55"    "56-60"   
# [9] "61-65"    "66-70"    "71-75"    "Above 46"

我想移动的水平“41-75”到“46以上”我该怎么办呢?

r dplyr statistics levels
1个回答
2
投票

从Tidyverse使用forcats

library(forcats)

ab <- data.frame(age = factor(levels = c("18-25", "26-30", "31-35", "36-40", "41-45", "46-50", "51-55", "56-60", "61-65", "66-70", "71-75", "Above 46")))

ab$age <- fct_collapse(ab$age, 
            "Above 46" = c("46-50", "51-55", "56-60", "61-65", "66-70", "71-75", "Above 46"))
© www.soinside.com 2019 - 2024. All rights reserved.