В Sun, 22 Sep 2024 07:44:16 -0400
Naresh Gurbuxani <naresh_gurbux...@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

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to