[Rcpp-devel] howto access elements of a DataFrame ?

Wasilios Goutas wasili at goutas.de
Mon May 29 21:46:32 CEST 2017


Hi KK,

thank you. This helped a lot. Also the stream operators is something I was also looking for :)
bye
Wasili



> Am 29.05.2017 um 20:21 schrieb Qiang Kou <qkou at umail.iu.edu>:
> 
> Hi, Wasilios,
> 
> I modified your code a little and paste below. I hope this helps. 
> 
> Best,
> 
> KK
> 
> 
> df <- data.frame(first=c("a","A", "aA"), second=c("b","B","bB"), third=rep(c("c","C","cC"),4))
> PLAYGROUND = new.env()
> PLAYGROUND$testData <- split(df, df$first)
> 
> src_4='
> Rcpp::Environment e(env);
> Rcpp::List splitted ( e["testData"] );
> Rprintf("splitted contains %d entries \\n", splitted.size());
> for(int i = 0; i< splitted.size(); i++){
>   Rcpp::DataFrame df(splitted[i]);
>   Rcpp::Rcout << "element " << i << " has " << df.nrow() << " rows " << df.ncol() << " cols" << std::endl;
>   Rcpp::CharacterVector df_names = df.names();
>   Rcpp::Rcout << "col names: " << df_names << std::endl;
>   Rcpp::CharacterVector v = df[0];
>   Rcpp::Rcout << v << std::endl;
> }
> return splitted;
> '
> 
> library(inline)
> 
> fun_4 <- cxxfunction(sig=signature(env="environment"), body = src_4, plugin = "Rcpp")
> ret_4 <- fun_4(PLAYGROUND)
> 
> 
> On Sun, May 28, 2017 at 2:02 PM, Wasilios Goutas <wasili at goutas.de <mailto:wasili at goutas.de>> wrote:
> Hi,
> 
> I’m pretty new to Rcpp and tried some examples and was surprised how nice C++ can be included in R code :)
> 
> 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.
> 
> Below is an example I created to learn how to access the values out of a splitter data.frame.
> As you can see I use an environment object as parameter containing a splitted data.frame with the name ‚testData‘ in it.
> 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.
> 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.
> 
> 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 :)
> Thanks in advance
> Wasili
> 
> 
> (the commented lines crashes R ones uncommented)
> 
> df <- data.frame(first=c("a","A", "aA"), second=c("b","B","bB"), third=rep(c("c","C","cC"),4))
> PLAYGROUND = new.env()
> PLAYGROUND$testData <- split(df, df$first)
> 
> src_4='
>   Rcpp::Environment e(env);
>   Rcpp::List splitted ( e["testData"] );
>   Rprintf("splitted contains %d entries \\n", splitted.size());
>   for(int i = 0; i< splitted.size(); i++){
>     Rprintf("typeof splitted[%d] = %s \\n", i, typeid(splitted[i]).name());
>     Rcpp::DataFrame df = Rcpp::as<Rcpp::DataFrame>(splitted[i]);
> //    Rprintf("typeof df = %s \\n", i, typeid(df).name());
>     Rprintf(" element %d has %d rows \\n", i, df.nrow());
>     for(int k=0; k < df.nrow(); k++){
> //      Rcpp::GenericVector v = df[k];
> //      Rprintf("typeof v = %s \\n", typeid(v).name());
> //      Rprintf(" row [%d] = %s \\n", k, df[k]);
>     }
>   }
>   return splitted;
> '
> rm(fun_4)
> fun_4 <- cxxfunction(sig=signature(env="environment"), body = src_4, plugin = "Rcpp")
> ret_4 <- fun_4(PLAYGROUND)
> 
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org <mailto:Rcpp-devel at lists.r-forge.r-project.org>
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel <https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
> 
> 
> -- 
> Qiang Kou
> qkou at umail.iu.edu <mailto:qkou at umail.iu.edu>
> School of Informatics and Computing, Indiana University
> 

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


More information about the Rcpp-devel mailing list