在dplyr中使用变量列名称汇总 [英] using variable column names in dplyr summarise
问题描述
我发现这个问题已经问了,但没有适当的答案。 R在dplyr的summary函数中使用变量列名称
I found this question already asked but without proper answer. R using variable column names in summarise function in dplyr
我想计算两个列均值之间的差,但是列名应由变量提供...到目前为止,我只发现了函数 as.name
以文本形式提供列名,但这在某种程度上不起作用...
I want to calculate the difference between two column means, but the column name should be provided by variables... So far I found only the function as.name
to provide column names as text, but this somehow doesn't work here...
使用固定列名
x <- c('a','b')
df <- group_by(data.frame(a=c(1,2,3,4), b=c(2,3,4,5), c=c(1,1,2,2)), c)
df %>% summarise(mean(a) - mean(b))
具有可变列,则无效
df %>% summarise(mean(x[1]) - mean(x[2]))
df %>% summarise(mean(as.name(x[1])) - mean(as.name(x[2])))
由于这是3年前的要求,并且 dplyr
处于良好的发展之中,我想知道现在是否有答案。
Since this was asked already 3 years ago and dplyr
is under good development, I am wondering if there is an answer to this now.
推荐答案
您可以使用 base :: get
:
df %>% summarise(mean(get(x[1])) - mean(get(x[2])))
# # A tibble: 2 x 2
# c `mean(a) - mean(b)`
# <dbl> <dbl>
# 1 1 -1
# 2 2 -1
get
将默认在当前环境中搜索。
get
will search in current environment by default.
如错误消息所示,平均值
需要逻辑或数字对象, as.name
返回一个名称:
As the error message says, mean
expects a logical or numeric object, as.name
returns a name:
class(as.name("a")) # [1] "name"
您可以评估自己的名字,也可以使用它:
You could evaluate your name, that would work as well :
df %>% summarise(mean(eval(as.name(x[1]))) - mean(eval(as.name(x[2]))))
# # A tibble: 2 x 2
# c `mean(eval(as.name(x[1]))) - mean(eval(as.name(x[2])))`
# <dbl> <dbl>
# 1 1 -1
# 2 2 -1
这篇关于在dplyr中使用变量列名称汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!