Try this: aggregate(height ~ class + group + name, data = myData, FUN = mean)
On Wed, Oct 6, 2010 at 4:13 PM, Geoffrey Smith <g...@asu.edu> wrote: > Hello, I am having trouble getting the output from the tapply function > formatted so that it can be made into a nice table. Below is my question > written in R code. Does anyone have any suggestions? Thank you. Geoff > > #Input the data; > name <- c('Tom', 'Tom', 'Jane', 'Jane', 'Enzo', 'Enzo', 'Mary', 'Mary'); > year <- c(2008, 2009, 2008, 2009, 2008, 2009, 2008, 2009); > group <- c('A', 'A', 'B', 'B', 'A', 'A', 'B', 'B'); > class <- c(0, 0, 0, 0, 1, 1, 1, 1); > height <- c(62, 63, 59, 58, 67, 66, 70, 71); > > #Combine the data into a data frame; > myData <- data.frame(name, year, group, class, height); > myData; > > #Calculate the mean of height by class, group, and name; > tapply(myData$height, data.frame(myData$class, myData$group, myData$name), > mean); > > #The raw output from the tapply function is fine, but I would; > #really like the output to look like this; > # class group name mean > # 0 A Tom 62.5 > # 0 B Jane 58.5 > # 1 A Enzo 66.5 > # 1 B Mary 70.5 > > -- > Geoffrey Smith > Visiting Assistant Professor > Department of Finance > W. P. Carey School of Business > Arizona State University > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
______________________________________________ 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.