Split, Apply, Recombine

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# split up the data by gear number
data <- split(mtcars, mtcars$gear)
# apply a function (in this case a linear model)
fits <- lapply(data, function(x) return(lm(x$mpg~x$disp)$coef))
# recombine the function applied in the line aboove again
do.call(rbind, fits)
  (Intercept)      x$disp
3    24.51557 -0.02577046
4    39.56753 -0.12221268
5    31.66095 -0.05077512