[R] Aggregate with Function List ?

David Winsemius dwinsemius at comcast.net
Wed Mar 7 16:47:00 CET 2012


On Mar 7, 2012, at 10:36 AM, Michael Karol wrote:

> All:
>
> It now appears to be working; however, I have a new problem.  The  
> format of the output is not what I need.  Currently it produces an  
> output as three columns; "Group.1", "Group.2", and "x"
> whereas I need a data frame with columns of Day, Hour, mean, sd,  
> median, min, max.

Just anme the categories in the by list and the function:

> Interestingly, the output appears on the console looking like the  
> data frame I need with 7 columns, but internally it only has 3.
> I'm using R version 2.14.0 (2011-10-31)
>
>> MeansByDayTime
>   Group.1 Group.2          x.1          x.2          x.3          x. 
> 4          x.5
> 1        1     0.0 0.0003333333 0.0005773503 0.0000000000  
> 0.0000000000 0.0010000000
> 2        8     0.0 0.0003333333 0.0005773503 0.0000000000  
> 0.0000000000 0.0010000000
> snipped

>
> My example data set and script follows.
>
> # Create a Test Data Frame
> Subject <-c(1001,1001,1001,1001,1001,1001,1001,1001,1001,
>            1002,1002,1002,1002,1002,1002,1002,1002,1002,
>            1003,1003,1003,1003,1003,1003,1003,1003,1003,
>            1001,1001,1001,1001,1001,1001,1001,1001,1001,
>            1002,1002,1002,1002,1002,1002,1002,1002,1002,
>            1003,1003,1003,1003,1003,1003,1003,1003,1003 )
> Day <- c (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
>          8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 )
> Hour <- c(0,0.5,1,2,2.5,3,4,6,12,
>          0,0.5,1,2,2.5,3,4,6,12,
>          0,0.5,1,2,2.5,3,4,6,12,
>          0,0.5,1,2,2.5,3,4,6,12,
>          0,0.5,1,2,2.5,3,4,6,12,
>          0,0.5,1,2,2.5,3,4,6,12 )
> Conc <-  
> c 
> (0,2,3,2,2.9,7.5,4,3,2,0,2.2,3.2,1.9,3,8,3,2.3,3.4,0.001,3,2,1.7,5,9,6,3.6,1.1 
> ,
>          
> 0.001,3.4,2.5,4.2,3.8,9.2,4.7,4.1,1.1,0,2.5,3.8,3.2,3.7,5,1.8,1.9,6.3,0,8,6,3.2,5.3,8.2,5.5,2.2,0.9 
> )
> DF <- data.frame(cbind(Subject,Day,Hour,Conc))
> #----------------------------------------------------------------------------------------------------------------------------
> # ----------- Compute Mean Concentration by Day and Hour.

#Edited code:

MeansByDayTime <- aggregate(as.double(DF$Conc),
                              by = list(Day = DF$Day, Hour=DF$Hour),
       FUN =
       function(x) c( mean =    mean(x, trim = 0, na.rm = T,  
weights=NULL),
                       sd =     sd(x, na.rm=TRUE),
                       median = median(x, na.rm=TRUE),
                       min =    min(x, na.rm=TRUE),
                       max =    max(x, na.rm=TRUE)
                    )
         )
# Show results
MeansByDayTime
>
>
>>
>>
>> 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