head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
# List of subsets
iris1 = split(iris, iris$Species)

# Function to apply on each subset
f = function(x) {
  fit = lm(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, data = x)
  summary(fit)
}

# Apply function
lapply(iris1, f)
## $setosa
## 
## Call:
## lm(formula = Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, 
##     data = x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.17450 -0.05866 -0.03017  0.06594  0.32950 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  -0.29474    0.21717  -1.357   0.1813  
## Petal.Length  0.16913    0.08560   1.976   0.0542 .
## Sepal.Width   0.01984    0.05642   0.352   0.7267  
## Sepal.Length  0.04504    0.06196   0.727   0.4710  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1002 on 46 degrees of freedom
## Multiple R-squared:  0.1509, Adjusted R-squared:  0.09556 
## F-statistic: 2.726 on 3 and 46 DF,  p-value: 0.05489
## 
## 
## $versicolor
## 
## Call:
## lm(formula = Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, 
##     data = x)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.271031 -0.066139  0.002672  0.063371  0.246702 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.16864    0.18946  -0.890 0.378043    
## Petal.Length  0.30875    0.05350   5.771 6.37e-07 ***
## Sepal.Width   0.22328    0.06187   3.609 0.000756 ***
## Sepal.Length -0.07398    0.04742  -1.560 0.125599    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1105 on 46 degrees of freedom
## Multiple R-squared:  0.7069, Adjusted R-squared:  0.6878 
## F-statistic: 36.98 on 3 and 46 DF,  p-value: 2.571e-12
## 
## 
## $virginica
## 
## Call:
## lm(formula = Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, 
##     data = x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.51430 -0.17403  0.00328  0.16883  0.44503 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.50026    0.39454   1.268 0.211192    
## Petal.Length  0.14968    0.12084   1.239 0.221751    
## Sepal.Width   0.43869    0.11698   3.750 0.000493 ***
## Sepal.Length -0.09258    0.10803  -0.857 0.395875    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2348 on 46 degrees of freedom
## Multiple R-squared:  0.3136, Adjusted R-squared:  0.2689 
## F-statistic: 7.006 on 3 and 46 DF,  p-value: 0.00056