best way to convert DataFrame to Matrix in RCpp

2020-08-17 18:32发布

问题:

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.

回答1:

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;
}


回答2:

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.



标签: r rcpp