[R] does there any function like sumif in excel?

David Winsemius dwinsemius at comcast.net
Wed Nov 2 20:10:40 CET 2011


On Nov 2, 2011, at 9:06 AM, Celine wrote:

> I have a similar problem
> 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 ?

You could get sums within groups using the ave() function. It would  
put values on all rows. Was there a reason you only wanted values on  
the first? Then if you wanted to make the non-first elements NA that  
could also be done.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/does-there-any-function-like-sumif-in-excel-tp858444p3972963.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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list