[R] Convert the output of by() to a data frame

David Winsemius dwinsemius at comcast.net
Tue Feb 8 06:50:07 CET 2011


On Feb 7, 2011, at 11:29 PM, Al Roark wrote:

>
> I'd like to summarize several variables in a data frame, for  
> multiple groups, and store the results in a data.frame. To do so,  
> I'm using by(). For example:
>
> df<- 
> data 
> .frame 
> (a 
> =1:10,b=11:20,c=21:30,grp1=c("x","y"),grp2=c("x","y"),grp3=c("x","y"))
> dfsum<-by(df[c("a","b","c")], df[c("grp1","grp2","grp3")], range)
>
> The result has a class of "by" and a mode of "list". I'm new to R  
> and can't find any documentation on this class, and don't see  
> methods for it associated with the as.data.frame.  How should I go  
> about coercing this to a data frame?  Is there a comprehensive  
> source that I'm might be missing, which can tell me such things?

I looked it up in Spector's "Data Manipulation":

 > do.call(rbind, dfsum)
      [,1] [,2]
[1,]    1   29
[2,]    2   30

Your test case is a bit pathological, since there are really only two  
"combinations" within your three groups of factors, at least only two  
combinations with values. Quite frankly, I have never understood how  
by() works and do most of my work with tapply and  
Hmisc::formula.describe and aggreagate. Many people have switched over  
the plyr package.

>
> Cheers
> 		 	   		
> 	[[alternative HTML version deleted]]


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list