Skip to content
Prev 32997 / 398506 Next

converting "by" to a data.frame?

Since I don't have your by.df to test with I may not have it exactly 
right, but something along these lines should work:

byFits <- lapply(split(by.df,paste(by.df$A,by.df$B)),
                  FUN=function(data.) {
                     tmp <- coef(lm(y~x,data.))
                     data.frame(A=unique(data.$A),
                                B=unique(data.$B),
                                intercept=tmp[1],
                                slope=tmp[2])
                    })

byFitsDF <- do.call('rbind',byFits)

That's assuming I've got all the closing parantheses in the right 
places, since my email software (Eudora) doesn't do R syntax checking!

This approach can get rather slow if by.df is big, or when the 
computations in FUN are extensive (or both).

If by.df$A has mode character (as opposed to being a factor), then 
replacing A=unique(data.$A) with A=I(unique(data.$A)) might improve 
performance. You want to avoid character to factor conversions when 
using an approach like this.

-Don
At 2:54 PM -0700 6/5/03, Spencer Graves wrote: