Skip to content
Prev 166583 / 398502 Next

Applying 'lm' in each group

On 1/10/2009 12:55 PM, Bhargab Chattopadhyay wrote:
mdf <- data.frame(y,x,g)

lapply(split(mdf, g), function(X){lm(y ~ x, data = X)})
$`1`

Call:
lm(formula = y ~ x, data = X)

Coefficients:
(Intercept)            x
      76.11        10.85


$`2`

Call:
lm(formula = y ~ x, data = X)

Coefficients:
(Intercept)            x
    166.693        3.580


$`3`

Call:
lm(formula = y ~ x, data = X)

Coefficients:
(Intercept)            x
    218.412       -0.735