Skip to content
Prev 33029 / 398506 Next

converting "by" to a data.frame?

Spencer,
   Would "sapply" be better here?

R> by.df <- data.frame(A=rep(c("A1", "A2"), each=3),
R+                     B=rep(c("B1", "B2"), each=3),
R+                     x=1:6, y=rep(0:1, length=6))
R> t(sapply(split(by.df, do.call("paste", c(by.df[, 1:2], sep = ":"))),
R+          function(x) coef(lm(y ~ x, data = x))))
       (Intercept)             x
A1:B1   0.3333333 -1.517960e-16
A2:B2   0.6666667  3.282015e-16
R>

Sundar
Spencer Graves wrote: