John Merrill — written Jan 22, 2013 — source
Data frames are one of R’s distinguishing features. Exposing a list of lists as an array of cases, they make many formal operations such as regression or optimization easy to represent.
The R data.frame operation for lists is quite slow, in large part because it exposes a vast amount of functionality. This sample shows one way to write a much faster data.frame creator in C++ if one is willing to forego that generality.
Here is the result of comparing the native function to this version.
test replications elapsed relative 2 CheapDataFrameBuilder(a) 500 0.104 1.0 1 as.data.frame(a) 500 16.730 160.9
There are some subtleties in this code:
— It turns out that one can’t send super-large data frames to it because of possible buffer overflows. I’ve never seen that problem when I’ve written Rcpp functions which exchanged SEXPs with R, but this one uses Rcpp:export in order to use sourceCpp.
— Notice the invocation of clone() in the first line of the code. If you don’t do that, you wind up side-effecting the parameter, which is not what most people would expect.Tweet