【问题标题】:how to move data from one level to another如何将数据从一个级别移动到另一个级别
【发布时间】:2019-02-02 21:36:25
【问题描述】:

这是我的数据

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 以上”我该怎么做?

【问题讨论】:

  • “将 X 移入 Y”究竟是什么意思?是否要将 X 中的数据追加到 Y 中,然后删除 X?
  • levels(ab$age)[levels(ab$age) %in% c("46-50", "51-55", "56-60", "61-65", "66-70", "71-75", "Above 46")] <- "Above 46"

标签: r dplyr statistics levels


【解决方案1】:

使用 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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 2018-07-27
    • 2016-05-11
    • 1970-01-01
    • 2020-10-08
    • 2021-11-24
    相关资源
    最近更新 更多