R keep rows with at least one column greater than value

旧巷老猫 提交于 2019-11-26 08:36:47

问题


Say I have a data frame with a few hundred rows and a few hundred columns. How would I keep rows that have at least one value greater than 10?


回答1:


You can use rowSums to construct the condition in base R:

df[rowSums(df > 10) >= 1, ]

with dplyr (0.7.0), now you can use filter_all like this:

library(dplyr)
filter_all(df, any_vars(. > 10))



回答2:


This is another option:

df[apply(df>10,1,any),]



回答3:


We can use lapply with Reduce

df[Reduce(`|`, lapply(df, `>`, 10)), ]

data

set.seed(24)
df <- as.data.frame(matrix(sample(1:12, 5*20, replace=TRUE), ncol=5))


来源:https://stackoverflow.com/questions/38273643/r-keep-rows-with-at-least-one-column-greater-than-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!