[R] Aggregate with Function List ?
Eik Vettorazzi
E.Vettorazzi at uke.de
Fri Feb 24 17:55:42 CET 2012
Hi Michael,
something like the following might be a starting point for aggregating
data using arbitrary lists of functions:
(it is lacking a method for data.frame objects)
maggregate<-function(...)UseMethod("maggregate")
maggregate.default<-function(x, FUN, ...){
tmp<-lapply(FUN,function(fct)aggregate(x,FUN=fct,...))
names(tmp)<-sapply(substitute(FUN), deparse)[-1]
r2<-data.frame(tmp[[1]][,1],sapply(tmp,"[",-1))
names(r2)[1]<-names(tmp[[1]])[1]
r2
}
maggregate.formula<-function(formula, data, FUN, ..., subset, na.action
= na.omit){
tmp<-lapply(FUN,function(fct)aggregate(formula,data,fct,...,na.action =
na.action))
names(tmp)<-sapply(substitute(FUN), deparse)[-1]
r2<-data.frame(tmp[[1]][,1],sapply(tmp,"[",-1))
names(r2)[1]<-names(tmp[[1]])[1]
r2
}
#using formula method
maggregate(Sepal.Length~Species,data=iris,FUN=c(mean,median,sd))
maggregate(Sepal.Length~Species,data=iris,FUN=list(mean,quantile))
#check if parameters are passed to quantile
maggregate(Sepal.Length~Species,iris,FUN=list(mean,quantile),probs=c(.25,.5,.75))
maggregate(iris$Sepal.Length,by=list(iris$Species),FUN=list(mean,quantile))
Cheers
Am 23.02.2012 19:41, schrieb Michael Karol:
> R Experts
>
>
>
> I wish to tabulate into one data frame statistics summarizing
> concentration data. The summary is to include mean, standard
> deviation, median, min and max. I wish to have summaries by Dose, Day
> and Time. I can do this by calling aggregate once for each of the
> statistics (mean, standard deviation, median, min and max) and then
> execute 4 merges to merging the 5 data frames into one. (Example
> aggregate code for mean only is shown below.)
>
> Can someone show me the coding to do this as one command, rather than
> 5 calls to aggregate and 4 merges. In other words, in essence, I'd like
> to present to "FUN =" a list of functions, so all the summary stats come
> back in one data frame. Your assistance is appreciated. Thank you.
>
>
>
> MeansByDoseDayTime <- aggregate(as.double(DF$Concentration), by =
> list(DF$Dose, DF$Day, DF$Time), FUN = mean, trim = 0, na.rm = T,
> weights=NULL)
>
>
>
>
>
> Regards,
>
> Michael
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
--
Eik Vettorazzi
Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790
--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):
Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg
Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus
More information about the R-help
mailing list