[R] Aggregate with Function List ?

Michael Karol MKarol at syntapharma.com
Mon Mar 5 23:20:54 CET 2012


Thank you for the suggestion.  Tried it as :

MeansByDoseDayTime <- aggregate(as.double(PK04Sub$Concentration.ng.mL.), by = list(PK04Sub$Dose.Level, PK04Sub$Day, PK04Sub$HourNominal), FUN =
       function(x) c( mean(x, trim = 0, na.rm = T, weights=NULL),
                      sd(x, na.rm=TRUE),
                      median(x, na.rm=TRUE),
                      min(x, na.rm=TRUE),
                      max(x, na.rm=TRUE)
                    )
         )

and got the following error message.

Problem in FUN(...X.sub.i...., all.indices = c(1, 2..: FUN did not always return a scalar 


Suggestions?

Regards, 
Michael


-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net] 
Sent: Thursday, February 23, 2012 1:55 PM
To: Michael Karol
Cc: r-help at r-project.org
Subject: Re: [R] Aggregate with Function List ?


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



More information about the R-help mailing list