问题
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