Applying by function to lm()

前端 未结 3 458
野的像风
野的像风 2021-01-13 01:38

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

3条回答
  •  轮回少年
    2021-01-13 02:04

    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.

提交回复
热议问题