按组添加总和的新列 [英] Add a new column of the sum by group
问题描述
假设我有一个这样的数据框:
Suppose I have a data frame like this:
set.seed(123)
df <- as.data.frame(cbind(y<-sample(c("A","B","C"),10,T), X<-sample(c(1,2,3),10,T)))
df <- df[order(df$V1),]
是否有一个简单的函数可以将 V2 与 V1 相加(或任何 FUN)并将其作为新列添加到 df 中,例如:
Is there a simply function to sum (or any FUN) V2 by V1 and add to df as a new column, such that:
df$sum <- c(6,6,8,8,8,8,6,6,6,6)
df
我可能会为此编写一个函数,但我必须经常这样做,并且最好知道实现这一点的最简单方法.
I may write a function for that, but I have to do that frequently and be better to know the simplest way to realize that.
推荐答案
我同意 @mnel 至少在他的第一点.我在他引用的答案中没有看到 ave
的演示,我认为这是最简单"的 base-R 方法.使用该 data.frame(cbind( ...))
构造应该被取缔,并且证明它的教师应该被剥夺他们的证书.
I agree with @mnel at least on his first point. I didn't see ave
demonstrated in the answers he cited and I think it's the "simplest" base-R method. Using that data.frame(cbind( ...))
construction should be outlawed and teachers who demonstrate it should be stripped of their credentials.
set.seed(123)
df<-data.frame(y=sample( c("A","B","C"), 10, T),
X=sample(c (1,2,3), 10, T))
df<-df[order(df$y),] # that step is not necessary for success.
df
df$sum <- ave(df$X, df$y, FUN=sum)
df
y X sum
1 A 3 6
6 A 3 6
3 B 3 8
7 B 1 8
9 B 1 8
10 B 3 8
2 C 2 6
4 C 2 6
5 C 1 6
8 C 1 6
这篇关于按组添加总和的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!