R keep rows with at least one column greater than value

前端 未结 3 727
再見小時候
再見小時候 2020-11-28 15:32

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?

相关标签:
3条回答
  • 2020-11-28 16:06

    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))
    
    0 讨论(0)
  • 2020-11-28 16:16

    This is another option:

    df[apply(df>10,1,any),]
    
    0 讨论(0)
  • 2020-11-28 16:23

    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))
    
    0 讨论(0)
提交回复
热议问题