I want to do a linear regression in R using the `lm()`

function. My data is an annual time series with one field for year (22 years) and another for state (50 states). I want to fit a regression for each state so that at the end I have a vector of lm responses. I can imagine doing for loop for each state then doing the regression inside the loop and adding the results of each regression to a vector. That does not seem very R-like, however. In SAS I would do a 'by' statement and in SQL I would do a 'group by'. What's the R way of doing this?

# R – Linear Regression and group by in R

linear-regressionlmrregression

## Best Solution

Here's an approach using the plyr package: