Just had a conversation with coworkers about this, and we thought it’d be worth seeing what people out in SO land had to say. Suppose I had a list with N elements, where each element was a vector of length X. Now suppose I wanted to transform that into a data.frame. As with most things in R, there are multiple ways of skinning the proverbial cat, such as as.dataframe, using the plyr package, comboing do.call with cbind, pre-allocating the DF and filling it in, and others.
The problem that was presented was what happens when either N or X (in our case it is X) becomes extremely large. Is there one cat skinning method that’s notably superior when efficiency (particularly in terms of memory) is of the essence?
Since a
data.frameis already a list and you know that each list element is the same length (X), the fastest thing would probably be to just update theclassandrow.namesattributes:Update – this is ~2x faster than creating
d:Update 2 – I forgot about memory consumption. The last update makes two copies of
e. Using theattributesfunction reduces that to only one copy.