7

I have a large list (200.000) entries. Each contains a 16x3 tibble. What is the fastest way to combine all these tibbles to one large tibble instead of the large list?

The list looks like this :

x <- list( a = tibble(some_char = rep("pens", 16), 
                      some_int = rep(1, 16), 
                      some_other_int = rep(14, 16)),
                 b = tibble(some_char = rep("rubber", 16), 
                            some_int = rep(5, 16), 
                            some_other_int = rep(9, 16)))
)
mugdi
  • 365
  • 5
  • 17
  • 2
    I don't think the code example you gave is representative to the problem you described. Also, there are some parentheses missing, consider revising your question. – OTStats Apr 20 '20 at 12:10
  • if you check the array of answers here https://stackoverflow.com/questions/2851327/convert-a-list-of-data-frames-into-one-data-frame , most of the options should work – StupidWolf Apr 20 '20 at 12:12

3 Answers3

8

I think you can have a try to passing this large list into data.table::rbindlist()

Frank Zhang
  • 1,670
  • 7
  • 14
4

With

l <- list( 
  a = tibble(some_char = rep("pens", 16), some_int = rep(1, 16), some_other_int = rep(14, 16)),
  b = tibble(some_char = rep("rubber", 16), some_int = rep(5, 16), some_other_int = rep(9, 16))
)

you can use dplyr::bind_rows(l)

robertdj
  • 909
  • 1
  • 5
  • 10
4

Using purrr::map?


library(tibble)
library(purrr)
library(dplyr)

x <- list( a = tibble(some_char = rep("pens", 16), 
                      some_int = rep(1, 16), 
                      some_other_int = rep(14, 16)),
                 b = tibble(some_char = rep("rubber", 16), 
                            some_int = rep(5, 16), 
                            some_other_int = rep(9, 16)))

                 
x_combined <- map_dfr(x, bind_rows)                                  

#or if you want to include the source tibble id:

x_combined_id <- map_dfr(x, bind_rows, .id = "tib")

Peter
  • 11,500
  • 5
  • 21
  • 31