[R] filtering in aggregate

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 15 03:07:48 CET 2006

Here is an example using the builtin data set, CO2, where we
aggregate uptake over Treatment for Plant equal to "Qn1" and
Type equal to "Quebec".

with(subset(CO2, subset = Plant == "Qn1" & Type == "Quebec"),
  aggregate(list(uptake = uptake), list(Treatment = Treatment), sum))

or equivalently:

with(CO2[CO2$Plant == "Qn1" & CO2$Type == "Quebec",],
  aggregate(list(uptake = uptake), list(Treatment = Treatment), sum))


CO2a <- CO2[CO2$Plant == "Qn1" & CO2$Type == "Quebec",]
aggregate(CO2a[,"uptake", drop=FALSE], CO2a[,"Treatment", drop=FALSE], sum)

On 3/14/06, Antonio Olinto <aolinto_r at bignet.com.br> wrote:
> Hello all,
> I have a data frame with year, month, species, fishing gear and catch
> (Y, M, S, F, C) and I want the sum of C by Y for species "A" and fishing
> gear "trawl".
> I tried things like aggregate(C[S=="A" & F=="trawl"], list (Year =
> Y[S=="A" & F=="trawl"]), fun=sum), but it didn't worked.
> To overcome this problem I did a subset as a new data frame and then I
> used aggregate. But I'm sure there's a way to apply a filter in data frame.
> Thanks for any help. Best regards.
> Antonio Olinto
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list