Thanks Kevin, <br>This question came because when you do this<br><br>// [[Rcpp::export]]<br>DataFrame updateDFByValue(DataFrame df) {<br>    int N = df.nrows();<br>    NumericVector newCol(N,1.);<br>    df["newCol"] = newCol;<br>
    return(df);<br>}<br><br>The DataFrame is returned to R as a list, and building back another data.frame might <br><ol><li>cost time</li><li>appear like a waste if what was intended was to update the data.frame<br></li>
</ol><a href="http://cran.r-project.org/web/packages/data.table/index.html"></a><a> data.table</a> allows by-reference updates in R but there is no C api that I know of, but it is an enhanced data.frame so Rcpp deals with it as a data.frame, I thought it was too bad to be able to update by reference in R and not in C++ so I asked this genuine question.<br>
<br>Your way makes sense to me, I'll try to dig deeper.<br>Thanks <br><br>PS: Dirk answered me <a href="http://stackoverflow.com/questions/15731106/passing-by-reference-a-data-frame-and-updating-it-with-rcpp">here</a><br>
<br><div class="gmail_quote">2013/3/31 Kevin Ushey <span dir="ltr"><<a href="mailto:kevinushey@gmail.com" target="_blank">kevinushey@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">I think the problem here is that the assignment df["newCol"] = newCol copies the dataframe. Note that something like this would work as expected:<div class="im"><br><br><span style="font-family:arial,sans-serif;font-size:12.727272033691406px">#include <Rcpp.h></span><br style="font-family:arial,sans-serif;font-size:12.727272033691406px">

<span style="font-family:arial,sans-serif;font-size:12.727272033691406px">using namespace Rcpp;</span><br style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br style="font-family:arial,sans-serif;font-size:12.727272033691406px">

<span style="font-family:arial,sans-serif;font-size:12.727272033691406px">// [[Rcpp::export]]</span><br style="font-family:arial,sans-serif;font-size:12.727272033691406px"><span style="font-family:arial,sans-serif;font-size:12.727272033691406px">void updateDFByRef(DataFrame& df) {</span><br style="font-family:arial,sans-serif;font-size:12.727272033691406px">

<span style="font-family:arial,sans-serif;font-size:12.727272033691406px">    int N = df.nrows();</span><br style="font-family:arial,sans-serif;font-size:12.727272033691406px"><span style="font-family:arial,sans-serif;font-size:12.727272033691406px">    NumericVector newCol(N,1.);</span><br style="font-family:arial,sans-serif;font-size:12.727272033691406px">

</div><span style="font-family:arial,sans-serif;font-size:12.727272033691406px">    df[0] = newCol; // replace the 1st vector with the numeric vector of 1s, by ref</span><br style="font-family:arial,sans-serif;font-size:12.727272033691406px">

<span style="font-family:arial,sans-serif;font-size:12.727272033691406px">    return;</span><br style="font-family:arial,sans-serif;font-size:12.727272033691406px"><span style="font-family:arial,sans-serif;font-size:12.727272033691406px">}</span><div>

<font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">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.<br>

<br>I'm not sure if there's a ready-made solution, but I imagine the easiest way to do it would be:<br><br>1) Declare a new list of df.size()+1,<br>2) Copy the pointers to the new list (not sure the best way to do this in Rcpp),<br>

3) Assign the vector you want to the new, last column,<br>4) Return that new list.<br></font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">This should work since internally, lists (VECSXP)s are just vectors of SEXPs (pointers) to other R vectors (REALSXPs, INTSXPs, and so on...)</font></div>

<div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">(Please correct me if I'm wrong on the above.)</font></div><div><font face="arial, sans-serif"><br></font></div><div>
<font face="arial, sans-serif">-Kevin</font></div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">On Sun, Mar 31, 2013 at 6:44 AM, stat quant <span dir="ltr"><<a href="mailto:statquant@outlook.com" target="_blank">statquant@outlook.com</a>></span> wrote:<br>

</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">Hello list,<br>looking at Rcpp::DataFrame in <a href="http://gallery.rcpp.org/tags/dataframe/" target="_blank">the gallery</a> I realized that I didn't know how to modify a DataFrame by reference. Googling a bit I found <a href="http://stackoverflow.com/questions/13773529/passing-a-data-table-to-c-functions-using-rcpp-and-or-rcpparmadillo" target="_blank">this post on SO</a> and this <a href="http://www.mail-archive.com/rcpp-devel@lists.r-forge.r-project.org/msg04919.html" target="_blank">post on the archive</a>.<br>


There is nothing obvious so I suspect I miss something big like "It is already the case because" or "it does not make sense because".<br><br>I tried the following which compiled but the data.frame object passed to   updateDFByRef in R stayed untouched<br>


<br>#include <Rcpp.h><br>using namespace Rcpp;<br><br>// [[Rcpp::export]]<br>void updateDFByRef(DataFrame& df) {<br>    int N = df.nrows();<br>    NumericVector newCol(N,1.);<br>    df["newCol"] = newCol;<br>


    return;<br>}<br><br>Could somebody explain me what I am missing or kindly point me to a document where I can find the explanation ?<br><br>Cheers<br>
<br></div></div>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>
</blockquote></div><br>