-2

R program

I need to sum values from same names which are repeated in a list.

I have a list like,

  Person  Money
1  1        3
2  2        1
3  1        2
4  3        1
5  2        1

I need,

  Person  Money
1  1    (3+2=)5
2  2        2
3  3        1
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Marina
  • 11
  • 1
  • 1
  • 3

2 Answers2

2

Here is a solution with ddply from plyr

library(plyr)
z=data.frame(ddply(YourDataFrame,.(Person),summarise))
Duck
  • 39,058
  • 13
  • 42
  • 84
1

Here is a solution using the base function tapply.

df = data.frame(Person=c("Joe","Joe","Fred","Jane","Fred","Jane"), Money=c(2,5,2,1,7,2))
tapply(df$Money, df$Person, FUN=sum)
Jeffrey Evans
  • 2,325
  • 12
  • 18