我想做一些参数化的dplyr操作。表达问题根源的最简单的可复制示例如下:
# Data
test <- data.frame(group = rep(1:5, each = 2),
value = as.integer(c(NA, NA, 2, 3, 3, 5, 7, 8, 9, 0)))
> test
group value
1 1 NA
2 1 NA
3 2 2
4 2 3
5 3 3
6 3 5
7 4 7
8 4 8
9 5 9
10 5 0
# Summarisation example, this is what I'd like to parametrise
# so that I can pass in functions and grouping variables dynamically
test.summary <- test %>%
group_by(group) %>%
summarise(group.mean = mean(value, na.rm = TRUE))
> test.summary
Source: local data frame [5 x 2]
group group.mean
<int> <dbl>
1 1 NaN
2 2 2.5
3 3 4.0 # Correct results
4 4 7.5
5 5 4.5,,这就是我一个人走了多远
# This works fine, but notice there's no 'na.rm = TRUE' passed in
doSummary <- function(d_in = data, func = 'mean', by = 'group') {
# d_in: data in
# func: required function for summarising
# by: the variable to group by
# NOTE: the summary is always for the 'value' column in any given dataframe
# Operations for summarise_
ops <- interp(~f(value),
.values = list(f = as.name(func),
value = as.name('value')))
d_out <- d_in %>%
group_by_(by) %>%
summarise_(.dots = setNames(ops, func))
}
> doSummary(test)
Source: local data frame [5 x 2]
group mean(value)
<int> <dbl>
1 1 NA
2 2 2.5
3 3 4.0
4 4 7.5
5 5 4.5尝试使用“na.rm”参数
# When I try passing in the 'na.rm = T' parameter it breaks
doSummary.na <- function(d_in = data, func = 'mean', by = 'group') {
# Doesn't work
ops <- interp(~do.call(f, args),
.values = list(f = func,
args = list(as.name('value'), na.rm = TRUE)))
d_out <- d_in %>%
group_by_(by) %>%
summarise_(.dots = setNames(ops, func))
}
> doSummary.na(test)
Error: object 'value' not found 非常感谢你的帮助!
发布于 2016-06-20 11:24:56
您的标题提到了...,但您的问题没有提到。如果我们不需要处理...,答案就会容易得多,因为我们根本不需要do.call,我们可以直接调用函数;只需将ops定义替换为:
ops = interp(~f(value, na.rm = TRUE),
f = match.fun(func), value = as.name('value'))请注意,这里使用的是match.fun而不是as.name。这通常是一个更好的想法,因为它的工作“就像R”的函数查找。因此,不仅可以传递函数名字符作为参数,还可以传递函数名或匿名函数:
doSummary.na(test, function (x, ...) mean(x, ...) / sd(x, ...)) # x̂/s?! Whatever.说到这一点,您设置列名的尝试也失败了;您需要将ops放到列表中来解决这个问题:
d_in %>%
group_by_(by) %>%
summarise_(.dots = setNames(list(ops), func))…因为.dots需要一个操作列表( setNames也需要一个向量/列表)。但是,如果要将func对象传递给不是字符向量的函数,则此代码再次无法工作。要使它更健壮,请使用以下内容:
fname = if (is.character(func)) {
func
} else if (is.name(substitute(func))) {
as.character(substitute(func))
} else {
'func'
}
d_in %>%
group_by_(by) %>%
summarise_(.dots = setNames(list(ops), fname))如果您实际上希望允许传递...而不是已知的参数,事情就会变得更加复杂,因为(据我所知)根本没有通过interp传递...的直接方法,而且,和您一样,我无法让do.call方法工作。
包提供了非常好的函数make_call,它可以帮助我们找到解决方案。以上内容也可以写成
# Not good. :-(
ops = make_call(as.name(func), list(as.name('value'), na.rm = TRUE))这个很管用。,但只有在将func作为字符向量传递时,才会使用func。正如上面所解释的,这是不灵活的。
然而,make_call只是简单地封装了基R的as.call,我们可以直接使用它:
ops = as.call(list(match.fun(func), as.name('value'), na.rm = TRUE))现在我们可以简单地传递...:
doSummary = function (d_in = data, func = 'mean', by = 'group', ...) {
ops = as.call(list(match.fun(func), as.name('value'), ...))
fname = if (is.character(func)) {
func
} else if (is.name(substitute(func))) {
as.character(substitute(func))
} else {
'func'
}
d_in %>%
group_by_(by) %>%
summarize_(.dots = setNames(list(ops), fname))
}需要明确的是:使用interp可以实现同样的目标,但我认为这需要从列表手动构建formula对象,这相当于在我的解决方案中执行非常相同的操作,然后(冗余地)对结果调用interp。
我通常会发现,虽然非常优雅,但在某些情况下,基R提供了更简单的解决方案。特别是,interp是一个功能强大的substitute替代品,但是bquote,一个使用不足的基本R函数,已经提供了许多相同的语法好处。对象的最大好处是它们带着它们的计算环境,这与基本的R表达式不同。然而,这一点并不总是必要的。
https://stackoverflow.com/questions/37815891
复制相似问题