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:
Dear R-Help:
I want to (a) subset a data.frame by several columns, (b) fit a model to each subset, and (c) store a vector of results from the fit in the columns of a data.frame. In the past, I've used "for" loops do do this. Is there a way to use "by"?
Consider the following example:
> byFits <- by(by.df, list(A=by.df$A, B=by.df$B), + function(data.)coef(lm(y~x, data.))) > byFits A: A1 B: B1 (Intercept) x 3.333333e-01 -1.517960e-16 ------------------------------------------------------------ A: A2 B: B1 NULL ------------------------------------------------------------ A: A1 B: B2 NULL ------------------------------------------------------------ A: A2 B: B2 (Intercept) x 6.666667e-01 3.282015e-16
############################# Desired output:
data.frame(A=c("A1","A2"), B=c("B1", "B2"), .Intercept.=c(1/3, 2/3), x=c(-1.5e-16, 3.3e-16))
What's the simplest way to do this? Thanks, Spencer Graves
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help