[R] Aggregating data (with more than one function)

bogdan romocea br44114 at gmail.com
Thu Apr 21 17:25:54 CEST 2005


I am looking for an answer to a similar question - a generalized
solution that would be able to apply
   (1) any number of functions
   (2) to any number of vectors
   (3) by any number of factors 
(just like SQL's group by). 
The output data frame must contain the values of the by factors, to be
used for joins.

Aggregate() does (2) and (3). The solutions posted to this thread
(split+sapply, by, tapply) do (1) and (3) (or so it seems to me). What
would be the best way to get to (1)+(2)+(3)?

I am inclined to use aggregate() in a loop with 
eval(parse(text="aggregate expression here")). 
Running
groupby <- do.call("rbind", by(var_i, list(a,b,c,d,e,f),
	function(x) c(fct1(x),fct2(x),fct3(x),fct4(x))))
in a loop (var_1, var_2 etc) would be very nice but I don't know how
to add a-f as columns in the output data frame.

Thank you,
b.



On Mon, 2005-03-28 at 19:15 -0600, Sivakumaran Raman wrote:
> I have the data similar to the following in a data frame:
>     LastName   Department  Salary
> 1   Johnson    IT          56000
> 2   James      HR          54223
> 3   Howe       Finance     80000
> 4   Jones      Finance     82000
> 5   Norwood    IT          67000
> 6   Benson     Sales       76000
> 7   Smith      Sales       65778
> 8   Baker      HR          56778
> 9   Dempsey    HR          78999
> 10  Nolan      Sales       45667
> 11  Garth      Finance     89777
> 12  Jameson    IT          56786
> 
> I want to calculate both the mean salary broken down by Department and 
> also the
> total amount paid out per department i.e. I want both sum(Salary) and
> mean(Salary) for each Department. Right now, I am using aggregate.data.frame
> twice, creating two data frames, and then combining them using data.frame.
> However, this seems to be very memory and processor intensive and is 
> taking a
> very long time on my data set. Is there a quicker way to do this?
> 
> Thanks in advance,
> Siv Raman
> 
> ______________________________________________
> 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
>

______________________________________________
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