观测值小于特定值R的下降因子 [英] Dropping factors which levels have observations smaller than a specific value-R
本文介绍了观测值小于特定值R的下降因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我有这样的数据帧(df1),其因素有:
Let I have such data frame(df1) with factors:
factor1 factor2 factor3
------- ------- -------
d a x
d a x
b a x
b c x
b c y
c c y
c n y
c n y
c n y
我想从这个数据框中删除其中一个元素少于3个观察值的因子.
I want to drop factors from this data frame which one of elements have less than 3 observations.
在此数据帧中,factor1具有3个级别(d,b和c).但是d级别具有频率2.因此我想从该数据帧中删除factor1.
In this data frame factor1 has 3 levels(d,b and c). However d level has frequency 2. So I want to drop factor1 from this data frame.
结果数据框应为:
factor2 factor3
------- -------
a x
a x
a x
c x
c y
c y
n y
n y
n y
如何使用R做到这一点?我会很高兴为您提供任何帮助.非常感谢.
How can I do this using R? I will be very glad for any help. Thanks a lot.
推荐答案
您可以尝试使用lapply
和table
:
df1[, lapply(c(1,2,3), FUN = function(x) min(table(df1[,x]))) >= 3]
,并且更通用一些:
df1[, lapply(1:ncol(df1), FUN = function(x) min(table(df1[,x]))) >= 3]
这篇关于观测值小于特定值R的下降因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文