filtering with multiple conditions on many columns using dplyr

前端 未结 7 1497
野趣味
野趣味 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:43

    I wanted to see this was possible using standard evaluation with dplyr's filter_. It turns out it can be done with the help of interp from lazyeval, following the example code on this page. Essentially, you have to create a list of the interp conditions which you then pass to the .dots argument of filter_.

    library(lazyeval)
    
    dots <- lapply(target_columns, function(cols){
        interp(~y >= 0, .values = list(y = as.name(cols)))
    })
    
    filter_(df, .dots = dots)   
    
      id  sth1 tg1_num sth2 tg2_num others
    1  1  dave       2   ca      35    new
    2  4 leroy       0   az      25    old
    3  5 jerry       4   mi      55    old
    

    Update

    Starting with dplyr_0.7, this can be done directly with filter_at and all_vars (no lazyeval needed).

    df %>%
         filter_at(vars(target_columns), all_vars(. >= 0) )
    
      id  sth1 tg1_num sth2 tg2_num others
    1  1  dave       2   ca      35    new
    2  4 leroy       0   az      25    old
    3  5 jerry       4   mi      55    old
    

提交回复
热议问题