SebKrantz / collapse

Advanced and Fast Data Transformation in R
https://sebkrantz.github.io/collapse/
Other
627 stars 33 forks source link

`rowbind` on list of vectors #576

Closed kylebutts closed 2 months ago

kylebutts commented 2 months ago

Hi Sebastian,

I'm wondering if it's possible for rowbind() to work on a list of vectors (of the same length)? It's possible to do with unlist2d, but my instincts were to use rowbind which errors

library(collapse)
#> collapse 2.0.13, see ?`collapse-package` or ?`collapse-documentation`
l <- list(1:3, 4:6, 7:9)
collapse::unlist2d(l, idcols = FALSE)
#>   V1 V2 V3
#> 1  1  2  3
#> 2  4  5  6
#> 3  7  8  9

try({ rowbind(l) })
#> Error in rowbind(l) : 
#>   Item 1 of input is not a data.frame, data.table or list

rowbind(
  lapply(l, function(x) as.data.frame(t(x)))
)
#>   V1 V2 V3
#> 1  1  2  3
#> 2  4  5  6
#> 3  7  8  9
SebKrantz commented 2 months ago

Hi Kyle, rowbind() is basically data.table::rbindlist() with some collapse specific features and slight performance improvements for list-columns, i.e., it only supports lists of lists. In your case, using qDF(do.call(rbind, l)) would be the most efficient solution. I don't think I'll make rowbind() internally dispathch to rbind() because it's very hard to control the behvior/output in such cases.

kylebutts commented 2 months ago

thanks!