I am trying to aggregate data in column 2 to identifiers in col 1 eg.. take this> identifier quantity 1 10 1 20 2 30 1 15 2 10 3 20 and make this> identifier quantity 1 45 2 40 3 20 Thanks in advance for your help! -- View this message in context: http://r.789695.n4.nabble.com/Help-summarizing-R-data-frame-tp3069624p3069624.html Sent from the R help mailing list archive at Nabble.com.
see ?aggregate, and ?summaryBy (in package doBy) I think ddply (in package plyr) could also do the job Ivan Le 12/2/2010 17:24, chris99 a ?crit :> I am trying to aggregate data in column 2 to identifiers in col 1 > > eg.. > > take this> > > identifier quantity > 1 10 > 1 20 > 2 30 > 1 15 > 2 10 > 3 20 > > and make this> > > identifier quantity > 1 45 > 2 40 > 3 20 > > > Thanks in advance for your help!-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
Nice thing about R is there are a number of ways to do things:> xidentifier quantity 1 1 10 2 1 20 3 2 30 4 1 15 5 2 10 6 3 20> require(sqldf) > sqldf('select identifier, sum(quantity) as quantity from x group by identifier')identifier quantity 1 1 45 2 2 40 3 3 20>or using 'data.table'> require(data.table)Loading required package: data.table> x <- data.table(x) > x[, sum(quantity), by = identifier]identifier V1 [1,] 1 45 [2,] 2 40 [3,] 3 20 On Thu, Dec 2, 2010 at 11:24 AM, chris99 <cheakes at hotmail.com> wrote:> > I am trying to aggregate data in column 2 to identifiers in col 1 > > eg.. > > take this> > > identifier ? ? ? quantity > 1 ? ? ? ? ? ? ? ? ? ? 10 > 1 ? ? ? ? ? ? ? ? ? ? 20 > 2 ? ? ? ? ? ? ? ? ? ? 30 > 1 ? ? ? ? ? ? ? ? ? ? 15 > 2 ? ? ? ? ? ? ? ? ? ? 10 > 3 ? ? ? ? ? ? ? ? ? ? 20 > > and make this> > > identifier ? ? ? ? quantity > 1 ? ? ? ? ? ? ? ? ? ?45 > 2 ? ? ? ? ? ? ? ? ? ?40 > 3 ? ? ? ? ? ? ? ? ? ?20 > > > Thanks in advance for your help! > -- > View this message in context: http://r.789695.n4.nabble.com/Help-summarizing-R-data-frame-tp3069624p3069624.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Here are some examples with tapply, aggregate, ddply: x <- read.table("clipboard", head=TRUE) with(x, tapply(quantity, identifier, sum)) aggregate(x$quantity, by=list(x$identifier), sum) aggregate(quantity ~ identifier, data = x, sum) library(plyr) ddply(x, .(identifier), summarise, quantity=sum(quantity)) HTH Patrick Am 02.12.2010 17:24, schrieb chris99:> > I am trying to aggregate data in column 2 to identifiers in col 1 > > eg.. > > take this> > > identifier quantity > 1 10 > 1 20 > 2 30 > 1 15 > 2 10 > 3 20 > > and make this> > > identifier quantity > 1 45 > 2 40 > 3 20 > > > Thanks in advance for your help!
Try this: with(DF, rowsum(quantity, identifier)) On Thu, Dec 2, 2010 at 2:24 PM, chris99 <cheakes@hotmail.com> wrote:> > I am trying to aggregate data in column 2 to identifiers in col 1 > > eg.. > > take this> > > identifier quantity > 1 10 > 1 20 > 2 30 > 1 15 > 2 10 > 3 20 > > and make this> > > identifier quantity > 1 45 > 2 40 > 3 20 > > > Thanks in advance for your help! > -- > View this message in context: > http://r.789695.n4.nabble.com/Help-summarizing-R-data-frame-tp3069624p3069624.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]