Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a large list into a data.frame

I am trying to convert a list of 13,000 elements, each element being a zoo object with nr = 230 and ncol = 4, to a dataframe.

I have tried setattr(mylist, 'class', 'data.frame'), but it resulted in a huge vector of NAs. I have also tried the quickdf(mylist) function from the plyr package, but that didn't work either.

The do.call(rbind.data.frame, mylist)type methods are very slow, thus not an option in this case.

Any suggestion as to the most efficient method to convert such a list to a dataframe?

like image 746
Mayou Avatar asked Oct 26 '25 03:10

Mayou


1 Answers

Use rbindlist from the data.table package.

data <- matrix(data = 1, nrow = 230, ncol = 4)
lstData <- rep(list(data), 16000)

library(data.table)
lstData <- Map(as.data.frame, lstData)
dfrData <- rbindlist(lstData)


system.time(dfrData <- rbindlist(lstData))
user  system elapsed 
0.12    0.03    0.15 
like image 167
Wolfgang Wu Avatar answered Oct 28 '25 17:10

Wolfgang Wu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!