[英]How to remove rows where columns satisfy certain condition in data frame
我有一个看起来像这样的数据框
df <- data.frame(cbind(1:10, sample(c(1:5), 10, replace=TRUE)))
# in real case the columns could be more than two
# and the column name could be anything.
我要删除的所有行的所有列的值都小于5。这是怎么做的?
df[!apply(df,1,function(x)all(x<5)),]
首先...请停止使用cbind
创建data.frames。 如果继续,您将感到抱歉。 R会惩罚你。
df[ !rowSums(df <5) == length(df), ]
(length()函数返回数据帧中的列数。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.