[英]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.