best way to convert DataFrame to Matrix in RCpp

二次信任 提交于 2019-12-10 02:28:49

问题


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.



来源:https://stackoverflow.com/questions/24352208/best-way-to-convert-dataframe-to-matrix-in-rcpp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!