filtering with multiple conditions on many columns using dplyr

前端 未结 7 1474
野趣味
野趣味 2021-01-02 02:05

I\'ve searched on SO trying to find a solution to no avail. So here it is. I have a data frame with many columns, some of which are numerical and should be non-negative. I w

7条回答
  •  时光取名叫无心
    2021-01-02 02:45

    Here's a possible vectorized solution

    ind <- grep("_num$", colnames(df))
    df[!rowSums(df[ind] < 0),]
    #   id  sth1 tg1_num sth2 tg2_num others
    # 1  1  dave       2   ca      35    new
    # 4  4 leroy       0   az      25    old
    # 5  5 jerry       4   mi      55    old
    

    The idea here is to create a logical matrix using the < function (it is a generic function which has data.frame method - which means it returns a data frame like structure back). Then, we are using rowSums to find if there were any matched conditions (> 0 - matched, 0- not matched). Then, we are using the ! function in order to convert it to a logical vector: >0 becomes TRUE, while 0 becomes FALSE. Finally, we are subsetting according to that vector.

提交回复
热议问题