Passing strings as arguments in dplyr verbs

前端 未结 3 864
时光取名叫无心
时光取名叫无心 2021-02-19 17:01

I would like to be able to define arguments for dplyr verbs

condition <- \"dist > 50\"

and then use these strings in

3条回答
  •  梦如初夏
    2021-02-19 17:19

    While they're working on that, here is a workaround using if:

    library(dplyr)
    library(magrittr)
    
    ds <- data.frame(attend = c(1:5,NA,7:9,NA,NA,12))
    
    filter_na <- FALSE
    
    filtertest <- function(x,filterTF = filter_na){
      if(filterTF) x else !(x)
    }
    
    ds %>%
      filter(attend %>% is.na %>% filtertest)
    
      attend
    1      1
    2      2
    3      3
    4      4
    5      5
    6      7
    7      8
    8      9
    9     12
    
    filter_na <- TRUE
    ds %>%
      filter(attend %>% is.na %>% filtertest)
    
      attend
    1     NA
    2     NA
    3     NA
    

提交回复
热议问题