Using R statistics add a group sum to each row [duplicate]
I have data frame df
like:
key entry x1
1 1 0.2
1 2 0.1
1 3 0.5
1 4 0.6
2 1 0.2
2 2 0.1
2 3 0.7
2 4 0.3
Each group is defined by key
and has the same number of entry
values. I would like to keep this table structure for subsequent use. I need to add a new column called sumx1
so that each row has the sum of x1
for the key group associated with that row.
In the above example the sum for key group 1 would be 0.2+0.1+0.5+0.6 = 1.4 so in the new column named sumx1
I need to enter 1.4 for each row that has key of 1.
I tried:
df["sumx1"] <- NA
df$sumx1 <- aggregate(df$sumx1, list(key=df$key), sum)
but this throws a warning error as it only gives me the sum per group.
Use ave
:
df$sumx1 <- ave(df$x1, df$key, FUN=sum)
The go-to package for this kind of data wrangling is plyr
.
require(plyr)
ddply(df, .(key), transform, sumx1=sum(x1))