[R] Odp: Is the aggregate function the best way to do this?
petr.pikal at precheza.cz
Wed Feb 17 10:18:20 CET 2010
r-help-bounces at r-project.org napsal dne 17.02.2010 09:36:45:
> I'm having a dataframe 'Subset1' with a number of factor variables and
> numerical variables
> Now I want to make sums for all rows that have the same values for the
> different factor variables, except for the factor variables:
> who may have the same values.
> With the formula given below this works great, but in a situation with
> rows and 13 factor variables the calculation takes more than 2 minutes.
> So my question is: Does anyone knows if there exists a faster
I believe plyr package has optimised code for such aggregations. But I do
not use it myself much often so I am not sure.
You probably could speed things by avoiding ncol(Subset1) in aggregate.
Either use numbers or do
selection <- Subset1[,(ncol(Subset1)-159):ncol(Subset1)]
and avoiding unnecessary coercion to data.frame. Aggregate perform it for
Subset1.AGG <- aggregate(selection, list(VAR1 =
Subset1$VAR1,VAR2=Subset1$VAR2,VAR3 = Subset1$VAR3), FUN=sum)
> Subset1.AGG <-
> list(VAR1 = Subset1$VAR1,VAR2=Subset1$VAR2,VAR3 = Subset1$VAR3),
> Thank you very much for helping me out,
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help