[R] Sum with condition
S Ellison
S.Ellison at LGCGroup.com
Wed Nov 2 19:48:14 CET 2011
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}}
More information about the R-help
mailing list