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

Wasilios Goutas wasili at goutas.de
Sun May 28 23:02:15 CEST 2017


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

(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)

  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;
fun_4 <- cxxfunction(sig=signature(env="environment"), body = src_4, plugin = "Rcpp")
ret_4 <- fun_4(PLAYGROUND)

More information about the Rcpp-devel mailing list