在dplyr中使用udf进行标准评估 [英] Using standard evaluation with a udf in dplyr

查看:103
本文介绍了在dplyr中使用udf进行标准评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dplyr进行编程,因此我正在使用标准评估。我使用数据框架和列名称作为参数的通用函数。在函数中,我想在数据框的列上应用另一个我自己写的函数。这是一个最小的例子:

  some_udf<  -  function(x)mean(x + 3)
generic_function< ; - function(dat,input_var){
dat%>%dplyr :: summarise_(mean_3 = sprintf(some_udf(%s),input_var))
}

运行泛型函数时,我会收到以下错误:

  generic_function(mtcars,'cyl')

错误:找不到函数some_udf

当$ code> some_udf 被基本R函数替换时,像意味着 sd ,一切正常。



有人可以向我解释为什么udf不在这个案例和解决方案可能是什么?

解决方案

这实际上是这个问题,它没有被接受的ans但是正如David Arenburg和MrFlick的评论所指出的那样,您需要将表达式作为公式传递给正确的环境环境:

  library(dplyr)

some_udf< - function(x)mean(x + 3)
generic_function< - function(dat,input_var){
dat%>%
summarise_(mean_3 = as.formula(sprintf(〜some_udf(%s),input_var))
}

generic_function(mtcars ,'cyl')
#mean_3
#1 9.1875


I am programming with dplyr and therefore I am using standard evaluation. I make a generic function with a data frame and a column name as arguments. Within the function I would like to apply another function that I wrote myself on the column of the data frame. Here is a minimal example:

some_udf <- function(x) mean(x + 3)
generic_function <- function(dat, input_var){
  dat %>% dplyr::summarise_(mean_3 = sprintf("some_udf(%s)", input_var))
}

Now when running the generic function I'll get the following error:

generic_function(mtcars, 'cyl')

Error: could not find function "some_udf"

When some_udf is replaced by a base R function, like mean or sd, all works fine.

Could someone explain to me why the udf is not working in this case and what a solution might be?

解决方案

This is essentially a duplicate of this question, which doesn't have an accepted answer, but as pointed out in the comments by David Arenburg and MrFlick, you need to pass the expression as a formula so that the correct environment context is available:

library(dplyr)

some_udf <- function(x) mean(x + 3)
generic_function <- function(dat, input_var){
    dat %>% 
        summarise_(mean_3 = as.formula(sprintf("~some_udf(%s)", input_var)))
}

generic_function(mtcars, 'cyl')
#   mean_3
# 1 9.1875

这篇关于在dplyr中使用udf进行标准评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆