[R] tapply output

Phil Spector spector at stat.berkeley.edu
Wed Oct 6 21:27:48 CEST 2010


Geoffrey -
    The output you want is exactly what the aggregate() function
provides:

> aggregate(myData$height, myData[c('class','group','name')],mean)
   class group name    x
1     1     A Enzo 66.5
2     0     B Jane 58.5
3     1     B Mary 70.5
4     0     A  Tom 62.5

It should be mentioned that converting tapply's output to this form
isn't too difficult:

> tt = tapply(myData$height, data.frame(myData$class, myData$group, myData$name),
+             mean)
> answer = as.data.frame(as.table(tt))
> subset(answer,!is.na(Freq))
    myData.class myData.group myData.name Freq
2             1            A        Enzo 66.5
7             0            B        Jane 58.5
12            1            B        Mary 70.5
13            0            A         Tom 62.5

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Wed, 6 Oct 2010, Geoffrey Smith 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 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