Search code examples
rperformancelistdataframe

Fast way of converting large list to dataframe


I have a huge list (700 elements), each element being a vector of length = 16,000. I am looking for an efficient way of converting the list to a dataframe, in the following fashion (this is just a mock example):

lst <- list(a = c(1,2,3), b = c(4,5,6), c = c(7,8,9))

The end result I am looking for is:

 #  [,1] [,2] [,3]
 #a    1    2    3
 #b    4    5    6
 #c    7    8    9

This is what I have tried, but isn't working as I wish:

library(data.table)
result = rbindlist(Map(as.data.frame, lst))

What can I try? Bear in mind that my real example has huge dimensions, and I would need a rather efficient way of doing this operation.


Solution

  • Try this. We assume the components of L all are of the same length, n, and we also assume no row names:

    L <- list(a = 1:4, b = 4:1) # test input
    
    n <- length(L[[1]])
    DF <- structure(L, row.names = c(NA, -n), class = "data.frame")