<div dir="ltr">Hi, Wasilios,<div><br></div><div>I modified your code a little and paste below. I hope this helps. </div><div><br></div><div>Best,</div><div><br></div><div>KK</div><div><br></div><div><div><br></div><div>df <- data.frame(first=c("a","A", "aA"), second=c("b","B","bB"), third=rep(c("c","C","cC"),4))</div><div>PLAYGROUND = new.env()</div><div>PLAYGROUND$testData <- split(df, df$first)</div><div><br></div><div>src_4='</div><div>Rcpp::Environment e(env);</div><div>Rcpp::List splitted ( e["testData"] );</div><div>Rprintf("splitted contains %d entries \\n", splitted.size());</div><div>for(int i = 0; i< splitted.size(); i++){</div><div>  Rcpp::DataFrame df(splitted[i]);</div><div>  Rcpp::Rcout << "element " << i << " has " << df.nrow() << " rows " << df.ncol() << " cols" << std::endl;</div><div>  Rcpp::CharacterVector df_names = df.names();</div><div>  Rcpp::Rcout << "col names: " << df_names << std::endl;</div><div>  Rcpp::CharacterVector v = df[0];</div><div>  Rcpp::Rcout << v << std::endl;</div><div>}</div><div>return splitted;</div><div>'</div><div><br></div><div>library(inline)</div><div><br></div><div>fun_4 <- cxxfunction(sig=signature(env="environment"), body = src_4, plugin = "Rcpp")</div><div>ret_4 <- fun_4(PLAYGROUND)</div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 28, 2017 at 2:02 PM, Wasilios Goutas <span dir="ltr"><<a href="mailto:wasili@goutas.de" target="_blank">wasili@goutas.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
I’m pretty new to Rcpp and tried some examples and was surprised how nice C++ can be included in R code :)<br>
<br>
Currently I’m struggling with accessing the Elements of data.frames in C++ and hope you will give me a hint on how to get the values expected out of it.<br>
<br>
Below is an example I created to learn how to access the values out of a splitter data.frame.<br>
As you can see I use an environment object as parameter containing a splitted data.frame with the name ‚testData‘ in it.<br>
What I try to do is accessing the elements of the splitter data.frame one by one, but I’m not sure what kind of object type they are. It should be a kind of List (which seems to be a Vector in Rcpp) but I’m not sure.<br>
I read that a DataFrame is a list of vectors which all have the same length. but I was not able to find any information on how to get the column names and how to access the elements of the DataFrame object.<br>
<br>
It would be very nice if you could show me based on this code how to access the elements of the splitter data.frame in C++ and maybe also the column names :)<br>
Thanks in advance<br>
Wasili<br>
<br>
<br>
(the commented lines crashes R ones uncommented)<br>
<br>
df <- data.frame(first=c("a","A", "aA"), second=c("b","B","bB"), third=rep(c("c","C","cC"),4))<br>
PLAYGROUND = new.env()<br>
PLAYGROUND$testData <- split(df, df$first)<br>
<br>
src_4='<br>
  Rcpp::Environment e(env);<br>
  Rcpp::List splitted ( e["testData"] );<br>
  Rprintf("splitted contains %d entries \\n", splitted.size());<br>
  for(int i = 0; i< splitted.size(); i++){<br>
    Rprintf("typeof splitted[%d] = %s \\n", i, typeid(splitted[i]).name());<br>
    Rcpp::DataFrame df = Rcpp::as<Rcpp::DataFrame>(<wbr>splitted[i]);<br>
//    Rprintf("typeof df = %s \\n", i, typeid(df).name());<br>
    Rprintf(" element %d has %d rows \\n", i, df.nrow());<br>
    for(int k=0; k < df.nrow(); k++){<br>
//      Rcpp::GenericVector v = df[k];<br>
//      Rprintf("typeof v = %s \\n", typeid(v).name());<br>
//      Rprintf(" row [%d] = %s \\n", k, df[k]);<br>
    }<br>
  }<br>
  return splitted;<br>
'<br>
rm(fun_4)<br>
fun_4 <- cxxfunction(sig=signature(env=<wbr>"environment"), body = src_4, plugin = "Rcpp")<br>
ret_4 <- fun_4(PLAYGROUND)<br>
<br>
______________________________<wbr>_________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-<wbr>project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-<wbr>project.org/cgi-bin/mailman/<wbr>listinfo/rcpp-devel</a></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Qiang Kou<div><a href="mailto:qkou@umail.iu.edu" target="_blank">qkou@umail.iu.edu</a><br><div>School of Informatics and Computing, Indiana University</div><div><br></div></div></div></div>
</div></div></div>