简体   繁体   English

如何在R函数中将变量传递给过滤器函数

[英]How to pass variable to filter function within a R function

I am fairly new to R. I wrote the below function which tries to summarise a dataframe, based on a feature variable (passed to the function as 'variable') and a target variable (passed to the function as target_var ). 我对R相当陌生。我编写了以下函数,该函数尝试基于特征变量(作为“变量”传递给函数)和目标变量(作为target_var传递给函数)总结数据帧。 I also pass it a value ( target_val ) on which to filter. 我还传递了一个值( target_val )进行过滤。

The function below falls over on the filter line ( filter(target_var == target_val) ). 下面的函数落在过滤器行( filter(target_var == target_val) )上。 I think it has something to do with quo , quosure etc, but can't figure out how to fix it. 我认为它与quoquosure等有关,但无法弄清楚如何解决。 The following code should be ready to run - if you exclude the filter line it should work, if you included the filter line it will fall over. 以下代码应已准备就绪,可以运行-如果您排除过滤器行,那么它应该可以工作;如果您包括过滤器行,它将会掉下来。

library(dplyr)
target <- c('good', 'good', 'bad', 'good', 'good', 'bad')
var_1 <- c('debit_order', 'other', 'other', 'debit_order','debit_order','debit_order')

dset <- data.frame(target, var_1)
odds_by_var <- function(dataframe, variable, target_var, target_val){

  df_name <- paste('odds', deparse(substitute(variable)), sep = "_")
  variable_string <- deparse(substitute(variable))
  target_string <- deparse(substitute(target_var))

  temp_df1 <- dataframe %>%
    group_by_(variable_string, target_string) %>%
    summarise(cnt = n()) %>%
    group_by_(variable_string) %>%
    mutate(total = sum(cnt)) %>%
    mutate(rate = cnt / total) %>%
    filter(target_var == target_val) 

  assign(df_name, temp_df1, envir=.GlobalEnv)

}

odds_by_var(dset, var_1, target, 'bad')

so I assume you want to filter by target good or bad. 因此,我假设您要按目标好坏筛选。 In my understanding, always filter() before you group_by() , as you will possibly ommit your filter variables. 根据我的理解,请始终在group_by() filter()之前添加filter() ,因为您可能会省略过滤器变量。 I restructured your function a little: 我对您的功能进行了一些重组:

    dset <- data.frame(target, var_1)
odds_by_var <- function(dataframe, variable, target_var, target_val){

  df_name <- paste('odds', deparse(substitute(variable)), sep = "_")
  variable_string <- deparse(substitute(variable))
  target_string <- deparse(substitute(target_var))

  temp_df1 <- dataframe %>%
    group_by_(variable_string, target_string) %>%
    summarise(cnt = n()) %>%
    mutate(total = sum(cnt),
           rate = cnt / total) 
names(temp_df1) <- c(variable_string,"target","cnt","total","rate" )
temp_df1 <- temp_df1[temp_df1$target == target_val,]
  assign( df_name,temp_df1, envir=.GlobalEnv)

}

odds_by_var(dset, var_1, target, "bad")

result: 结果:

> odds_var_1
# A tibble: 2 x 5
# Groups:   var_1 [2]
  var_1       target   cnt total  rate
  <chr>       <chr>  <int> <int> <dbl>
1 debit_order bad        1     4  0.25
2 other       bad        1     2  0.5 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM