R中的组合因子水平 [英] Combining factor level in R

查看:27
本文介绍了R中的组合因子水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将级别A"、B"合并为A+B".我通过以下方式成功地做到了这一点:

I would like combine level "A","B" into "A+B". I successfully did this by the following:

x <- factor(c("A","B","A","C","D","E","A","E","C"))
x
#[1] A B A C D E A E C
#Levels: A B C D E
l <- c("A+B","A+B","C","D+E","D+E")
factor(l[as.numeric(x)])
#[1] A+B A+B A+B C   D+E D+E A+B D+E C  
#Levels: A+B C D+E

有没有更简单的方法来做到这一点?(即更易于解释的函数名称,例如 combine.factor(f, old.levels, new.levels) 将有助于更轻松地理解代码.)

Is there any more trivial way to do this? (i.e. more explainable function name such as combine.factor(f, old.levels, new.levels) would help to understand the code easier.)

另外,我试图找到一个命名良好的函数,它可能与 dplyr 包中的数据框一起使用,但没有运气.最接近的实现是

Also, I try to find a well named function which probably work with data frame in dplyr package but no luck. The closest implementation is

df %>% mutate(x = factor(l[as.numeric(x)]))

推荐答案

一个选项是 recode from car

One option is recode from car

library(car)
recode(x, "c('A', 'B')='A+B';c('D', 'E') = 'D+E'")
#[1] A+B A+B A+B C   D+E D+E A+B D+E C  
#Levels: A+B C D+E

它也应该与 dplyr

library(dplyr)
df %>%
   mutate(x= recode(x, "c('A', 'B')='A+B';c('D', 'E') = 'D+E'"))
#    x
#1 A+B
#2 A+B
#3 A+B
#4   C
#5 D+E
#6 D+E
#7 A+B
#8 D+E
#9   C

数据

df <- data.frame(x)

这篇关于R中的组合因子水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆