[英]How to remove rows where columns satisfy certain condition in data frame
I have a data frame that looks like this 我有一个看起来像这样的数据框
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.
What I want to do is to remove all rows where the value of all its columns is smaller than 5. What's the way to do it? 我要删除的所有行的所有列的值都小于5。这是怎么做的?
df[!apply(df,1,function(x)all(x<5)),]
First of all ...please stop using cbind
to create data.frames. 首先...请停止使用
cbind
创建data.frames。 You will be sorry if you continue. 如果继续,您将感到抱歉。 R will punish you.
R会惩罚你。
df[ !rowSums(df <5) == length(df), ]
(The length() function returns the number of columns in a dataframe.) (length()函数返回数据帧中的列数。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.