How to store filter expressions as strings?

后端 未结 2 1382
天涯浪人
天涯浪人 2020-12-21 06:51

For the analysis of a species database, I often need to change lots of criteria, depending on the projects scope etc.

As it is very inconvenient to always change the

2条回答
  •  情深已故
    2020-12-21 07:19

    In addition to @Konrad's methods, if the expression is a string, then we can use parse_expr from rlang

    library(rlang)
    library(dplyr)
    df1 %>% 
        filter(!! parse_expr(expr1))
    #   col_A col_B
    #1     A     1
    

    data

     df1 <- data.frame(col_A = LETTERS[1:10],
               col_B = 1:10,
               stringsAsFactors = FALSE)
    
    expr1 <-  "col_A == 'A' & col_B == 1"
    

提交回复
热议问题