Data.table version of Johns solution
library(data.table) Fits <- data.table(mtcars)[, .(MyFits = lapply(.SD, function(x) summary(lm(mpg ~ x)))), .SDcols = -1]
Some code explanations
data.table converts mtcars to a data.table object.SD also a data.table object that contains the columns you need to work with.SDcols = -1 tells .SD not to use the first column (since we don't want to put lm(mpg ~ mpg)lapply just runs the model across all columns in .SD (except for the one we skipped) and returns list objects of classes
Fit resume list, you can check them using
Fits$MyFits
But you can also work on them, for example, using the coef function on each attack
Fits[, lapply(MyFits, coef)]
Or get r.squered
Fits[, lapply(MyFits, '[[', "r.squared")]
David Arenburg
source share