Skip to content

Sum with condition

5 messages · Sarah Goslee, S Ellison, David Winsemius +1 more

#
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:
You'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:
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))
CELLCD        x
1   8928 94.30900
2   8929 84.87900
3   8930 40.13149
#
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?
This email and any attachments are confidential. Any use...{{dropped:8}}
#
On Nov 2, 2011, at 2:11 PM, Sarah Goslee wrote:

            
Celine. Please stop posting duplicates.
Or:
 > testdata$SumArea <- with(testdata, ave(AreaProtected, CELLCD,  
FUN=sum))
 > testdata
  CELLCD AreaProtected  SumArea
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