Skip to content
Prev 5591 / 10988 Next

[Rcpp-devel] DataFrame and passing by reference

I think the problem here is that the assignment df["newCol"] = newCol
copies the dataframe. Note that something like this would work as expected:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
void updateDFByRef(DataFrame& df) {
    int N = df.nrows();
    NumericVector newCol(N,1.);
    df[0] = newCol; // replace the 1st vector with the numeric vector of
1s, by ref
    return;
}

So, the reference to the original df is getting passed, the problem is
figuring out how to assign a new vector to df without forcing a copy.

I'm not sure if there's a ready-made solution, but I imagine the easiest
way to do it would be:

1) Declare a new list of df.size()+1,
2) Copy the pointers to the new list (not sure the best way to do this in
Rcpp),
3) Assign the vector you want to the new, last column,
4) Return that new list.

This should work since internally, lists (VECSXP)s are just vectors of
SEXPs (pointers) to other R vectors (REALSXPs, INTSXPs, and so on...)

(Please correct me if I'm wrong on the above.)

-Kevin
On Sun, Mar 31, 2013 at 6:44 AM, stat quant <statquant at outlook.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130331/65287659/attachment.html>