在R中组合因子水平 [英] Combining factor level in R
本文介绍了在R中组合因子水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
x< - 因子(c(A,B,A ,C,D,E,A,E,C))
x
#[1] ABACDEAEC
#Levels:ABCDE
l <-C(A + B,A + B,C,D + E,D + E)
因子(l [as.numeric(x)] )
#[1] A + B A + B A + BC D + E D + E A + B D + EC
#级别:A + BC D + E
有没有更简单的方法呢? (即更易解释的函数名称,例如 combine.factor(f,old.levels,new.levels)将有助于更容易地理解代码。)
另外,我尝试找到一个很好的命名函数,这可能与 dplyr 包中的数据框一起使用,但没有运气。最接近的实现是
df%>%mutate(x = factor(l [as.numeric(x)]))
解决方案
一个选项是 recode
从汽车
库(汽车)
recode(x,c('A','B')='A + B'; c('D','E')='D + E')
#[1 ] A + B A + B A + BC D + E D + E A + B D + EC
#级别:A + BC 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)
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
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.)
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)]))
解决方案
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
It should also work with 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
data
df <- data.frame(x)
这篇关于在R中组合因子水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文