我需要创建一个函数,该函数可以使用其名称来 group_by 和 summary 列。 我正在使用dplyr 0.4.1版(并且我无法更新),所以看来我在其他主题上找到的解决方案不起作用...
这是我的示例:
data <- data.frame(section=rep(c("A","B"),3), quantity=c(6:11))
#I need to get this result :
RESULT = data %>% group_by(section) %>% summarise(total=sum(quantity))
我实现了此功能,但出现错误:
# function :
synthetize = function(x,column,measure){
result = x %>% group_by(column) %>% summarise(total=sum(measure))
}
RESULT2=synthetize(data,column="section",measure="quantity")
RESULT2
我尝试了eval
,get
,但这似乎无济于事
答案 0 :(得分:1)
我们可以使用rlang::sym
将字符串转换为符号并评估(!!
)
library(tidyverse)
synthetize = function(x, column, measure){
x %>%
group_by_at(column) %>%
summarise(total=sum(!! rlang::sym(measure)))
}
synthetize(data, column="section", measure="quantity")
# A tibble: 2 x 2
# section total
# <fct> <int>
#1 A 24
#2 B 27
注意:这里我们使用OP的相同参数类型
如果我们使用的是dplyr
的旧版本,则以下内容可能会有所帮助
library(lazyeval)
synthetize2 = function(x, column, measure){
x %>%
group_by_(column) %>%
summarise(total = interp(~ sum(v1), v1 = as.name(measure)))
synthetize2(data, column='section', measure='quantity')
答案 1 :(得分:1)
另一种方法是使用enquo
:
library(tidyverse)
synthetize = function(x,column,measure) {
result = x %>% group_by(!! enquo(column)) %>% summarise(total := sum(!! enquo(measure)))
}
在这种情况下,您无需引用变量:
RESULT2 = synthetize(data, column = section, measure = quantity)
RESULT2
# A tibble: 2 x 2
section total
<fct> <int>
1 A 24
2 B 27
如果您无权访问最新的tidyverse
,请尝试使用get
:
library(dplyr)
synthetize = function(x,column,measure) {
result = x %>% group_by(get(column)) %>% summarise(total := sum(get(measure)))
}