I guess my problem is simple for most of you but I am new with R and I need some help, I have a dataframe : CELLCD AreaProtected 8928 52.39389 8928 41.91511 8929 21.21975 8929 63.65925 8930 26.08547 8930 14.04602 I wouldlike to sum the AreaProtected if it is the same CELLCD in another column : CELLCD AreaProtected SumAreaProtected 8928 52.39389 94.309 8928 41.91511 8929 21.21975 84,879 8929 63.65925 8930 26.08547 8930 14.04602 I am just started with R and I don't know how I can do that. Do you have any ideas ? Thanks a lot for your help, -- View this message in context: http://r.789695.n4.nabble.com/Sum-with-condition-tp3972839p3972839.html Sent from the R help mailing list archive at Nabble.com.
Hi, On Wed, Nov 2, 2011 at 8:44 AM, Celine <bellard.celine at gmail.com> wrote:> CELLCD AreaProtected > ? 8928 ? ? ?52.39389 > ? 8928 ? ? ?41.91511 > ? 8929 ? ? ?21.21975 > ? 8929 ? ? ?63.65925 > ? 8930 ? ? ?26.08547 > ? 8930 ? ? ?14.04602You'll need to figure out how you want it to be combined with the original data frame, since there can't be empty cells, but:> dput(testdata)structure(list(CELLCD = c(8928L, 8928L, 8929L, 8929L, 8930L, 8930L), AreaProtected = c(52.39389, 41.91511, 21.21975, 63.65925, 26.08547, 14.04602)), .Names = c("CELLCD", "AreaProtected"), class "data.frame", row.names = c(NA, -6L))> > aggregate(testdata$AreaProtected, by=list(CELLCD=testdata$CELLCD), FUN="sum")CELLCD x 1 8928 94.30900 2 8929 84.87900 3 8930 40.13149 -- Sarah Goslee http://www.functionaldiversity.org
If you used aggregate() on the data frame you would have a new data frame containing the sum of all AreaProtected for each CELLCD. For your mini-example, using d as your data frame, d2<-aggregate(d[,2], by=list(CELLCD=d$CELLCD),sum) d2 # CELLCD x # 1 8928 94.30900 # 2 8929 84.87900 # 3 8930 40.13149 If you then use merge() you get merge(d,d2) # CELLCD AreaProtected x #1 8928 52.39389 94.30900 #2 8928 41.91511 94.30900 #3 8929 21.21975 84.87900 #4 8929 63.65925 84.87900 #5 8930 26.08547 40.13149 #6 8930 14.04602 40.13149 Maybe one of those is what you want?> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Celine > Sent: 02 November 2011 12:44 > To: r-help at r-project.org > Subject: [R] Sum with condition > > I guess my problem is simple for most of you but I am new > with R and I need some help, I have a dataframe : > > CELLCD AreaProtected > 8928 52.39389 > 8928 41.91511 > 8929 21.21975 > 8929 63.65925 > 8930 26.08547 > 8930 14.04602 > > I wouldlike to sum the AreaProtected if it is the same CELLCD > in another column : > > CELLCD AreaProtected SumAreaProtected > 8928 52.39389 94.309 > 8928 41.91511 > 8929 21.21975 84,879 > 8929 63.65925 > 8930 26.08547 > 8930 14.04602 > > I am just started with R and I don't know how I can do that. > Do you have any ideas ? > > > Thanks a lot for your help, > > -- > View this message in context: > http://r.789695.n4.nabble.com/Sum-with-condition-tp3972839p397 > 2839.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. > *******************************************************************This email and any attachments are confidential. Any use...{{dropped:8}}