Re: [R] store list objects in data.table

2024-09-22 Thread Naresh Gurbuxani
After rereading Rui Barrades's reply, I was able to get "lm" object from data.table carsreg3 <- carsdt[, .(fit = list(lm(mpg ~ disp + hp + wt))), by = .(cyl)] carsreg3[, .N] #[1] 3 carsreg3[(cyl == 6), .(fit)][[1]][[1]] |> class() #[1] "lm" carsreg3[(cyl == 6), .(fit)][[1]][[1]] |> summary() car

Re: [R] store list objects in data.table

2024-09-22 Thread Ivan Krylov via R-help
В Sun, 22 Sep 2024 07:44:16 -0400 Naresh Gurbuxani пишет: > 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

Re: [R] store list objects in data.table

2024-09-22 Thread Naresh Gurbuxani
Thanks everyone for their responses. My data is organized in a data.table.  My goal is to perform analyses according to some groups.  The results of analysis are objects.  If these objects could be stored as elements of a data.table, this would help downstream summarizing of results. Let me

Re: [R] store list objects in data.table

2024-09-21 Thread Bert Gunter
Well, you may have good reasons to do things this way -- and you certainly do not have to explain them here. But you might wish to consider using R's poly() function and a basic nested list structure to do something quite similar that seems much simpler to me, anyway: x <- rnorm(20) df <- data.fr

Re: [R] store list objects in data.table

2024-09-21 Thread CALUM POLWART
I think there is a typo in your reprex l(x^2) ?? mydt[1,2] contains a list. Which when unlisted contains a load of data. I'm not sure what you are asking for? Are you trying to unlist that and have it as a row? Sort of pivot.wider if you like or unnest in tidyverse concepts? I think the data.tab

Re: [R] store list objects in data.table

2024-09-21 Thread Rui Barradas
Às 22:25 de 21/09/2024, Naresh Gurbuxani escreveu: I am trying to store regression objects in a data.table df <- data.frame(x = rnorm(20)) df[, "y"] <- with(df, x + 0.1 * x^2 + 0.2 * rnorm(20)) mydt <- data.table(mypower = c(1, 2), myreg = list(lm(y ~ x, data = df), lm(y ~ x + I(x^2), data = df