使用dplyr[復制]在groupby中使用變量列名

[英]Use function in groupby with variable column name in R using dplyr [duplicate]


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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/12/21/72520a69ca375ddf13643f11f3aa26ad.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com