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 <was...@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@lists.r-forge.r-project.org > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel -- Qiang Kou q...@umail.iu.edu School of Informatics and Computing, Indiana University
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel