Closed kendonB closed 4 years ago
One approach would be to merge the original data back in after unnesting. There might also be ways to improve performance somewhat before doing this. For instance,
library(microbenchmark)
library(data.table)
library(tidyfast)
library(broom)
dat = data.table(group=sample(1:500, 1e4, replace=TRUE),
y=rnorm(1e4),
x=rnorm(1e4))
fit = function(k) lm(y ~ x, k)
dat = dat[, .(data=.(.SD)), by=group][
, model := lapply(data, fit)][
, result := lapply(model, tidy)]
tf = function() dt_unnest(dat, result)
keep = function(x) dat[, result[[1L]], by=group][dat, on='group']
discard = function(x) dat[, result[[1L]], by=group]
all(tf() == discard())
#> [1] TRUE
microbenchmark(tf(), keep(), discard(),
times=100)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> tf() 9.661796 10.762439 14.220490 11.998833 16.118330 75.664467 100
#> keep() 1.987287 2.142959 2.453322 2.238526 2.380653 6.861493 100
#> discard() 1.040819 1.152247 1.350544 1.191882 1.265033 6.042629 100
Created on 2020-08-17 by the reprex package (v0.3.0)
Created on 2020-08-07 by the reprex package (v0.3.0)