1

Assume I have a dataframe:

Col1   Col2   Col3   Val
  a      1      a     2
  a      1      a     3
  a      1      b     5
  b      1      a     10
  b      1      a     20
  b      1      a     25

I want to aggregate across all columns and then attach this to my dataframe so that I have this output:

Col1   Col2   Col3   Val    Tot
  a      1      a     2      10
  a      1      a     3      10
  a      1      b     5      10
  b      1      a     10     55
  b      1      a     20     55
  b      1      a     25     55

Is there something I can do with tapply to accomplish this? I'm a little stuck on the most efficient way.

Jeffrey Kramer
  • 1,345
  • 6
  • 25
  • 43

2 Answers2

4

Assuming d is your dataframe:

> d$Tot <- ave(d$Val, d$Col1, FUN = sum)
> d
  Col1 Col2 Col3 Val Tot
1    a    1    a   2  10
2    a    1    a   3  10
3    a    1    b   5  10
4    b    1    a  10  55
5    b    1    a  20  55
6    b    1    a  25  55
Thomas
  • 43,637
  • 12
  • 109
  • 140
4

data.table approach

library(data.table)
setDT(df)[, Tot := sum(Val), by = Col1]

dplyr approach

library(dplyr)
df %>% 
  group_by(Col1) %>%
    mutate(Tot = sum(Val))
David Arenburg
  • 91,361
  • 17
  • 137
  • 196