相关疑难解决方法(0)

对多个dplyr过滤条件使用整齐的eval

我是整理eval并尝试编写泛型函数的新手 - 我现在正在努力的一件事就是为分类变量编写多个过滤条件.这就是我现在正在使用的 -

create_expr <- function(name, val){
   if(!is.null(val))
     val <- paste0("c('", paste0(val, collapse = "','"), "')")
   paste(name, "%in%", val)
}

my_filter <- function(df, cols, conds){
#   Args: 
#     df: dataframe which is to be filtered
#     cols: list of column names which are to be filtered
#     conds: corresponding values for each column which need to be filtered

cols <- as.list(cols)
conds <- as.list(conds)

args <- mapply(create_expr, cols, conds, SIMPLIFY = F)

if(!length(args))
  stop(cat("No filters provided"))

df <- df …
Run Code Online (Sandbox Code Playgroud)

r tidyverse tidyeval

5
推荐指数
1
解决办法
934
查看次数

标签 统计

r ×1

tidyeval ×1

tidyverse ×1