Skip to content
Prev 32995 / 398506 Next

converting "by" to a data.frame?

Hi, Thomas, et al.:

Thanks for the reply.  Unfortunately, "do.call" strips off the subset 
identifiers, which I want to use for further modeling:

 > do.call("rbind", byFits)
      (Intercept)              x
[1,]   0.3333333 -1.517960e-016
[2,]   0.6666667  3.282015e-016

The following does what I want using a "for" loop:

 > by.df <- data.frame(A=rep(c("A1", "A2"), each=3),
+  B=rep(c("B1", "B2"), each=3), x=1:6, y=rep(0:1, length=6))
 > by.lvls <- paste(as.character(by.df$A), as.character(by.df$B), sep=":")
 > A.B <- unique(by.lvls)
 > Fits <- data.frame(A.B = A.B, .Intercept.=rep(NA, length(A.B)),
+  x=rep(NA, length(A.B)))
 > Fits$A <- substring(A.B, 1, regexpr(":", A.B)-1)
 > Fits$B <- substring(A.B, regexpr(":", A.B)+1)
 > for(i in 1:length(A.B))
+  Fits[i, 2:3] <- coef(lm(y~x, by.df[by.lvls==A.B[i],]))
 > Fits
     A.B X.Intercept.             x  A  B
1 A1:B1    0.3333333 -1.517960e-16 A1 B1
2 A2:B2    0.6666667  3.282015e-16 A2 B2
 >

	  I wondered if there was something easier.

Thanks again for your reply.
Spencer Graves
Thomas Lumley wrote: