使用dplyr[复制]在groupby中使用变量列名

时间:2022-12-22 23:49:50

This question already has an answer here:

这个问题已经有了答案:

I have a dataframe:

我有一个dataframe:

df <- data.frame(Category = c(rep("A", 3), rep("B", 3)), Value = rnorm(6))
df
 Category       Value
1        A -0.94968814
2        A  2.56687061
3        A -0.15665153
4        B -0.47647105
5        B  0.83015076
6        B -0.03744522

Now I want to add another column which is the mean per Category. This can be done with the dplyr package really easy:

现在我想再加一列,也就是每个类别的均值。这可以用dplyr包非常简单:

df %>% group_by(Category) %>% 
  summarize(mean = mean(Value))

Now in piece of code my problem is: I can't use mean(Value), but I have a variable name that knows the column name: columnName = "Value" But this unfortunately won't work:

在这段代码中,我的问题是:我不能使用mean(Value),但是我有一个变量名,它知道列名:columnName = "Value"但不幸的是,这行不通:

columnName = "Value"

df %>% group_by(Category) %>% 
  summarize(mean = mean(columnName))

Warning messages: 1: In mean.default("Value") : argument is not numeric or logical: returning NA 2: In mean.default("Value") :
argument is not numeric or logical: returning NA

警告消息:1:在mean.default(“Value”)中:参数不是数字或逻辑:返回NA 2: In mean.default(“Value”):参数不是数字或逻辑:返回NA。

How can I pass the column name with the variable?

如何将列名与变量一起传递?

1 个解决方案

#1


2  

We can use get with aggregate

我们可以使用集合get

aggregate(get(columnName)~Category, df, mean)

#    Category get(columnName)
#1        A      -0.5490751
#2        B      -0.2594670

#1


2  

We can use get with aggregate

我们可以使用集合get

aggregate(get(columnName)~Category, df, mean)

#    Category get(columnName)
#1        A      -0.5490751
#2        B      -0.2594670