r - best way to convert DataFrame to Matrix in RCpp -
i have rcpp code, in part of code trying convert dataframe matrix. dataframe has numbers (no strings or dates).
the next code works:
//[[rcpp::export]] numericmatrix testdftonm1(dataframe x) { int nrows=x.nrows(); numericmatrix y(nrows,x.size()); (int i=0; i<x.size();i++) { y(_,i)=numericvector(x[i]); } homecoming y; }
i wondering if there alternative way (i.e. equivalent of as.matrix
in r) in rcpp same, similar next code below (which not work):
//[[rcpp::export]] numericmatrix testdftonm(dataframe x) { numericmatrix y(x); homecoming y; }
* edit *
thanks answers. dirk suggested, c++ code around 24x faster either of 2 answers , function
version 2% faster internal::convert_using_rfunction
version.
i looking reply within rcpp without calling r. should have made clear when posted question.
similar gabor's version, can this:
#include <rcpp.h> using namespace rcpp ; //[[rcpp::export]] numericmatrix testdftonm(dataframe x) { numericmatrix y = internal::convert_using_rfunction(x, "as.matrix"); homecoming y; }
r rcpp
No comments:
Post a Comment