2

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.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
cousin_pete
  • 578
  • 4
  • 15

3 Answers3

7

Use ave:

 df$sumx1 <- ave(df$x1, df$key, FUN=sum)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
2

The go-to package for this kind of data wrangling is plyr.

require(plyr)
ddply(df, .(key), transform, sumx1=sum(x1))
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
0
df <- data.frame(
    key=c(1, 1, 1, 1, 2, 2, 2, 2),
    entry=c(1, 2, 3, 4, 1, 2, 3, 4),
    x1=c(0.2, 0.1, 0.5, 0.6, 0.2, 0.1, 0.7, 0.3))

df$sumx1 <- sapply( df$key, function(key) { sum(df$x1[df$key==key]) } )
cbare
  • 12,060
  • 8
  • 56
  • 63