[R] Aggregate with Function List ?

Peter Ehlers ehlers at ucalgary.ca
Tue Mar 6 02:26:58 CET 2012


On 2012-03-05 14:20, Michael Karol wrote:
> 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?

Are you using an old version of R (prior to 2.11.0)?
Anyway, you might also check the plyr package's summarize() function.
Or check out the data.table package.

Peter Ehlers

>
> 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
>
> ______________________________________________
> 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.



More information about the R-help mailing list