create a formula in a data.table environment in R

前端 未结 1 1459
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 05:19

I would like to run a regression within a data.table. The formula needs to be constructed dynamically. I have tried the following method:

<
相关标签:
1条回答
  • 2020-12-05 06:05

    lm can accept a character string as the formula so combine that with .SD like this:

    > x[, as.list(coef(lm("a ~ b", .SD))), by = id]
       id (Intercept)  b
    1:  1          21 -1
    2:  2          21 -1
    3:  3          21 -1
    4:  4          21 -1
    5:  5          21 -1
    
    0 讨论(0)
提交回复
热议问题