[Rcpp-devel] Iterating an Rcpp::DataFrame

Dirk Eddelbuettel edd at debian.org
Thu Oct 9 14:33:24 CEST 2014


On 9 October 2014 at 14:07, Christian Authmann wrote:
| Hello,
| 
| this sounds like a newbie question, but I haven't been able to find a 
| good answer in Rcpp's documentation, source or testcases..
| 
| Say I have an internal data type which I want to wrap/unwrap as a DataFrame.
| 
| template<> SEXP wrap(const Foo &foo) {
|    Rcpp::DataFrame dataframe;
|    for (auto key : foo.keys()) {
|      if (key.is_numeric()) {
|        Rcpp::NumericVector vec;
|        // Fill vector with numbers
|        dataframe[ key ] = vec;
|      }
|      else if (key.is_string()) {
|        Rcpp::StringVector vec;
|        // Fill vector with strings
|        dataframe[ key ] = vec;
|      }
|      // etc
|    }
| }
| 
| template<> Foo as(SEXP sexp) {
|    Rcpp::DataFrame dataframe(sexp);
|    // now what?
| }
| 
| 
| First, I need to iterate over all vectors in the dataframe. I could 
| access a vector using
|    dataframe["my_key"]
| but that would require me to have a list of all the keys/column names 
| first. R has a colnames() function, but getting that into an 
| Rcpp::Function and calling it seems overly complicated..
| I couldn't find anything in Rcpp's source or testcases.. but maybe 
| there's a simpler way?

DataFrame objects are clunky, that's just the way it is.  It is a glorified
list of vectors, possibly of different types, with the added constraint of
equal length.  Not much more really.  

| Problem number two, how do I type the vector once I have it? The 
| dataframe can contain NumericVector or StringVector (or possibly 
| something different). Should I just construct each type, try/catch the 
| exceptions and see what fits, or is there a better way to determine the 
| type of a SEXP?

In my usage I typically assume that I know the layout. So if the third column
is, say, a vector of strings I would assign it to that. 

For free-form incoming data that is a little harder but I believe we have
several 'dispatching based on types' examples floating around here, on the
Rcpp Gallery and/or other places.

The joys of interfacing a dynamically-typed language with a statically-typed
one :)

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list