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. 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 3 1 0.5 2.4000000000 0.5291502622 2.2000000000 2.0000000000 3.0000000000 4 8 0.5 4.6333333333 2.9501412396 3.4000000000 2.5000000000 8.0000000000 5 1 1.0 2.7333333333 0.6429100507 3.0000000000 2.0000000000 3.2000000000 6 8 1.0 4.1000000000 1.7691806013 3.8000000000 2.5000000000 6.0000000000 7 1 2.0 1.8666666667 0.1527525232 1.9000000000 1.7000000000 2.0000000000 8 8 2.0 3.5333333333 0.5773502692 3.2000000000 3.2000000000 4.2000000000 9 1 2.5 3.6333333333 1.1846237096 3.0000000000 2.9000000000 5.0000000000 10 8 2.5 4.2666666667 0.8962886440 3.8000000000 3.7000000000 5.3000000000 11 1 3.0 8.1666666667 0.7637626158 8.0000000000 7.5000000000 9.0000000000 12 8 3.0 7.4666666667 2.1939310229 8.2000000000 5.0000000000 9.2000000000 13 1 4.0 4.3333333333 1.5275252317 4.0000000000 3.0000000000 6.0000000000 14 8 4.0 4.0000000000 1.9467922334 4.7000000000 1.8000000000 5.5000000000 15 1 6.0 2.9666666667 0.6506407099 3.0000000000 2.3000000000 3.6000000000 16 8 6.0 2.7333333333 1.1930353445 2.2000000000 1.9000000000 4.1000000000 17 1 12.0 2.1666666667 1.1590225767 2.0000000000 1.1000000000 3.4000000000 18 8 12.0 2.7666666667 3.0615900009 1.1000000000 0.9000000000 6.3000000000 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. MeansByDayTime <- aggregate(as.double(DF$Conc), by = list(DF$Day, DF$Hour), 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) ) ) # Show results MeansByDayTime Thank you Regards, Michael ================================================================================ -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Michael Karol Sent: Monday, March 05, 2012 5:21 PM To: David Winsemius Cc: r-help@r-project.org Subject: Re: [R] Aggregate with Function List ? 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:dwinsem...@comcast.net] Sent: Thursday, February 23, 2012 1:55 PM To: Michael Karol Cc: r-help@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@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. ______________________________________________ R-help@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.