10

I have an RCpp code, where in a part of code I am trying to convert a DataFrame to a Matrix. The DataFrame only has numbers (no strings or dates).

The following code works:

//[[Rcpp::export]]
NumericMatrix testDFtoNM1(DataFrame x) {
  int nRows=x.nrows();  
  NumericMatrix y(nRows,x.size());
  for (int i=0; i<x.size();i++) {
    y(_,i)=NumericVector(x[i]);
  }  
  return y;
}

I was wondering if there is alternative way (i.e. equivalent of as.matrix in R) in RCpp to do the same, something similar to the following code below (which does NOT work):

//[[Rcpp::export]]
NumericMatrix testDFtoNM(DataFrame x) {
  NumericMatrix y(x);  
  return y;
}

* EDIT *

Thanks for the answers. As Dirk suggested, the C++ code is around 24x faster than either of the two answers and Function version is 2% faster than the internal::convert_using_rfunction version.

I was originally looking for an answer within RCpp without calling R. Should have made that clear when I posted my question.

uday
  • 6,453
  • 13
  • 56
  • 94
  • 3
    I'd go with the solution you have in your question. We may need a better way to get `n` as well as `k` from the data.frame (ie as length of names vector). Otherwise it is a good solution in my mind which I like as well as some of the suggested answers. – Dirk Eddelbuettel Jun 22 '14 at 15:03
  • And I was of course wrong about n and k retrievers as we do have `.size()`, I was mainly thinking of functions with similar names to those for matrices. May add that at some point... – Dirk Eddelbuettel Jun 22 '14 at 18:18

2 Answers2

9

Similar to Gabor's version, you can do something like this:

#include <Rcpp.h>
using namespace Rcpp ;

//[[Rcpp::export]]
NumericMatrix testDFtoNM(DataFrame x) {
  NumericMatrix y = internal::convert_using_rfunction(x, "as.matrix");  
  return y;
}
Romain Francois
  • 17,432
  • 3
  • 51
  • 77
7

If you don't mind calling back to R it can be done compactly like this:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix DF2mat(DataFrame x) {
    Function asMatrix("as.matrix");
    return asMatrix(x);
}

UPDATE Incorporated Romain's comment.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341