使用do.call与dplyr标准评估版本 [英] Using do.call with dplyr standard evaluation version
问题描述
如何使用参数和函数的变量列表来获取 do.call
,以便使用标准评估版本总结_
in dplyr?
How can I get a do.call
with a variable list of arguments and functions to work with the standard evaluation version of summarise_
in dplyr?
## Some sample data, function, and variables to interpolate
set.seed(0)
dat <- data.frame(a=runif(10), b=runif(10))
fn <- function(x, y) IQR(x / y, na.rm = TRUE)
funs <- list(fn="fn")
targs <- list("a", "b")
这是 lazyeval :: interp
我正在努力工作
library(dplyr)
interp(~do.call(fn, xs), .values=list(fn=funs$fn, xs=targs))
# ~do.call("fn", list("a", "b"))
但它不工作,
dat %>%
summarise_(out = interp(~do.call(fn, xs), .values=list(fn=funs$fn, xs=targs)))
预期结果
dat %>%
summarise(out = do.call(fn, list(a, b)))
# out
# 1 1.084402
如果我添加一些打印声明,我知道问题是a和b没有得到正确的解释,但是我无法弄清楚如何引用它们。
If I add in some print statements, I know the problem is that the "a" and "b" aren't being interpreted properly, but I haven't been able to figure out how to quote them properly.
fn <- function(x, y) { print(x); print(y); IQR(x / y, na.rm = TRUE) }
dat %>%
summarise_(out = interp(~do.call(fn, xs), fn=funs$fn, xs=targs))
# [1] "a"
# [1] "b"
# Error: non-numeric argument to binary operator
推荐答案
targs
参数需要为code>调用类。 调用
( a
和 b
)中的变量需要成为名称
类。所有这一切都在下面的第二(和第三)行中完成。 ?调用
,?as.name
和?is.language
可能使线更容易理解。
The targs
argument needs to be a call
class. The variables in the call
(a
and b
) need to be a name
class. All this is done in the second (and third) line below. ?call
, ?as.name
, and ?is.language
might make the line more understandable.
dat <- data.frame(a=runif(10), b=runif(10), grp=rep(1:2, each=5))
targs_quoted = do.call(call, c("list", lapply(targs, as.name)), quote=TRUE)
# In hardcoded form, targs_quoted = quote(list(a, b))
dat %>%
group_by(grp) %>%
summarise_(out = interp(~do.call(fn, xs),
.values=list(fn=funs$fn, xs=targs_quoted)))
# Source: local data frame [2 x 2]
#
# grp out
# (int) (dbl)
# 1 1 1.0754497
# 2 2 0.9892201
dplyr
的nse(非标准评估)小插曲在这里非常有帮助。我发现。
总是引用整个表,而不是分组表。这就是为什么评论中的一些建议没有按需要工作。
dplyr
's "nse" (non-standard evaluation) vignette was very helpful here. I found that the .
always referred to the entire table, not the grouped table. That's why some of the recommendations in the comments didn't "work" as wanted.
这篇关于使用do.call与dplyr标准评估版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!