[R] Adding same items together in data.frame

Dennis Murphy djmuser at gmail.com
Sat May 14 05:05:04 CEST 2011


Hi:

There are many ways to do this sort of thing in R; one way is (naming
your example data frame d)

aggregate(value ~ gene, data = d, FUN = sum)
  gene value
1   aa    74
2   bb    10
3   cc     9
4   dd   100
5   ee    55

This code line works for R-2.11.0 and later.

HTH,
Dennis

On Fri, May 13, 2011 at 4:06 PM, wong, honkit (Stephen)
<honkit at stanford.edu> wrote:
> Dear All,
> I am new to R. I have a 2 column data frame with more than ten thousand
> rows. Something like below. I want to add up all duplicated items, e.g. the
> three "aa" add up together to get a single value gene=a, value=74. How can I
> do that?? Thanks for help !
> gene value
> aa       20
> bb      10
> cc       9
> aa      30
> aa      24
> dd       100
> ee      55
> .... ...
> Millions thanks.
> Best Regards,
> hon
>
> WONG, Hon-Kit (Stephen)
> Cleary Lab, Dept of Pathology
> Stanford University
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list