An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120223/fb7023fc/attachment.pl>
Aggregate with Function List ?
3 messages · Michael Karol, David Winsemius, Eik Vettorazzi
On Feb 23, 2012, at 1:41 PM, Michael Karol wrote:
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.
Perhaps something like this?
MeansByDoseDayTime <- aggregate(as.double(DF$Concentration), by =
list(DF$Dose, DF$Day, DF$Time), FUN =
function(x) c( mean(x, trim = 0, na.rm = T, weights=NULL),
sd(x, na.rm=TRUE),
median(x, na.rm=TRUE),
min(na.rm=TRUE),
max(x, na.rm=TRUE)
)
)
David Winsemius, MD West Hartford, CT
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