Skip to content

Aggregate with Function List ?

3 messages · Michael Karol, David Winsemius, Eik Vettorazzi

#
On Feb 23, 2012, at 1:41 PM, Michael Karol wrote:

            
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: