Using data.table, I am trying to write a function that takes a data table, a formula object, and a string as arguments, and creates and stores multiple model objects.
You've to evaluate it within the environment .SD (as lm can not "see" V2 and V3 otherwise):
.SD
lm
SectRegress <- function (df,eq,sectors) { Output <- df[, list(model=list(lm(eq, .SD))), by=sectors] return(Output) } Test <- SectRegress(myData,formula(V2~V3),sectors="V1")