I am new to R and I am just learning about the apply
functions and how they work. I simply want to extract the coefficients from a lm
fit on a var
This is much easier in more modern packages, e.g. data.table
:
library(data.table)
setDT(d)
d[ , .(reg = list(lm(x ~ color))), by = year]
# year reg
# 1: 2012
# 2: 2006
# 3: 2011
# 4: 2008
# 5: 2007
# 6: 2010
# 7: 2009
The reg
column has lm
objects; note we need to wrap lm
in list(.)
so that data.table
doesn't confuse the plain list (note that is.list(lm(x ~ color, data = d))
is TRUE
.