[R] Adding same items together in data.frame

wong, honkit (Stephen) honkit at stanford.edu
Mon May 16 23:58:43 CEST 2011


Thanks !!  This aggregate function is really powerful to do this kind of
manipulation! Looks like R is quite versatile for text manipulation, I don't
need to learn Perl language.

Appreciate all your guys help!


WONG, Hon-Kit (Stephen)
Cleary Lab, Dept of Pathology
Stanford University
Lokey Stem Cell Research Building, Room G2035
259 Campus Drive
Stanford, CA  94305


-----Original Message-----
From: Dennis Murphy [mailto:djmuser at gmail.com] 
Sent: Friday, May 13, 2011 8:05 PM
To: wong, honkit (Stephen)
Cc: r-help at r-project.org
Subject: Re: [R] Adding same items together in data.frame


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