How to convert a huge list-of-vector to a matrix more efficiently?

user1787675 picture user1787675 · Nov 5, 2012 · Viewed 147.1k times · Source

I have a list of length 130,000 where each element is a character vector of length 110. I would like to convert this list to a matrix with dimension 1,430,000*10. How can I do it more efficiently?\ My code is :

output=NULL
for(i in 1:length(z)) {
 output=rbind(output,
              matrix(z[[i]],ncol=10,byrow=TRUE))
}

Answer

flodel picture flodel · Nov 5, 2012

This should be equivalent to your current code, only a lot faster:

output <- matrix(unlist(z), ncol = 10, byrow = TRUE)