【发布时间】:2016-12-11 20:04:46
【问题描述】:
我想只保留基于频率的前 2 个因素水平,并将所有其他因素分组到“其他”中。我试过了,但没有帮助。
df=data.frame(a=as.factor(c(rep('D',3),rep('B',5),rep('C',2))),
b=as.factor(c(rep('A',5),rep('B',5))),
c=as.factor(c(rep('A',3),rep('B',5),rep('C',2))))
myfun=function(x){
if(is.factor(x)){
levels(x)[!levels(x) %in% names(sort(table(x),decreasing = T)[1:2])]='Others'
}
}
df=as.data.frame(lapply(df, myfun))
预期输出
a b c
D A A
D A A
D A A
B A B
B A B
B B B
B B B
B B B
others B others
others B others
【问题讨论】:
-
您想计算整个数据框或列的因子的频率吗?请分享您的预期输出。
-
这仅适用于单个变量,我仅根据频率保留前 2 个因素,并将所有其他级别分组为其他。
-
给定上面的数据框,你能添加预期的输出吗?
-
@sotos 这会起作用,感谢hint.fun1
-
@是的,我明白了,但是我们必须首先按降序重新排列级别,然后执行您所做的操作。你可以把它当作一个答案。