data.table also has great tools for solving problems such as this:
library(data.table)
set.seed(1)
dat <- data.table(x=runif(100), y=runif(100), grp=rep(1:2,50))
dat[,coef(lm(y~x)),by=grp]
The first row in each group is the intercept, and the second row is the coefficient:
grp V1
[1,] 1 0.5991761
[2,] 1 -0.1350489
[3,] 2 0.4401174
[4,] 2 0.1400153
If you'de rather have a wide data.frame, that just takes a little more specification:
dat[,list(intercept=coef(lm(y~x))[1], coef=coef(lm(y~x))[2]),by=grp]
grp intercept coef
[1,] 1 0.5991761 -0.1350489
[2,] 2 0.4401174 0.1400153
Or you could put it even more succinctly as:
dat[,as.list(coef(lm(y~x))),by=grp]
(Intercept) x
1: 1 0.5991761 -0.1350489
2: 2 0.4401174 0.1400153