R-dplyr 0.4.1:如何通过函数中的列名进行汇总 [英] R - dplyr 0.4.1 : How to summarise by a column name in a function
问题描述
我需要创建一个函数,该函数可以使用其名称来 group_by 和 summary 列.我正在使用dplyr 0.4.1版(并且我无法更新),所以看来我在其他主题上找到的解决方案不起作用...
I need to create a function that could group_by and summarise a data frame using the names of its columns. I'm working with dplyr version 0.4.1 (and I cannot update), so it looks like the solutions I've found on the other topics doesn't work...
这是我的示例:
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))
我实现了此功能,但出现错误:
I implemented this function, but I got an error :
# 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
,但这似乎无济于事
I tried eval
, get
, but it looks like this doesn't help
推荐答案
我们可以使用 rlang :: sym
将字符串转换为符号并评估( !!
)
We can convert the string to symbol with rlang::sym
and evaluate (!!
)
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的相同参数类型
NOTE: Here we use the OP's same argument type
如果我们使用的是 dplyr
的旧版本,则以下内容可能会有所帮助
If we are using older version of dplyr
, may be the following would help
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')
这篇关于R-dplyr 0.4.1:如何通过函数中的列名进行汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!