[R] store list objects in data.table

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Sun Sep 22 14:09:46 CEST 2024


В Sun, 22 Sep 2024 07:44:16 -0400
Naresh Gurbuxani <naresh_gurbuxani using hotmail.com> пишет:

> carsreg <- carsdt[, .(fit = lm(mpg ~ disp + hp + wt)), by = .(cyl)]
> 
> #I would like a data.table with three rows, one each for "lm" object 
> corresponding to cyl value
> 
> carsreg[, .N]
> #[1] 36

Try wrapping the lm() expression in an extra list():

carsreg <- carsdt[, .(fit = list(lm(mpg ~ disp + hp + wt))), by = .(cyl)]
# [1] 3
carsreg
#      cyl      fit
#    <num>   <list>
# 1:     6 <lm[12]>
# 2:     4 <lm[12]>
# 3:     8 <lm[12]>

-- 
Best regards,
Ivan



More information about the R-help mailing list