STL vectorをRcpp::NumericMatrixのある列(行)に代入したい
生のstl vectorをNumericMatrixに突っ込もうとすると怒られた、一方、一旦Rcpp::NumericVectorを経由させたらイケた。
以下、サンプル。行・列ごとの代入もラクラクで便利。
library(Rcpp) library(inline) src <- ' std::vector<int> x(2,111); NumericMatrix xx(2, 5); xx(_,1) = NumericVector(x.begin(), x.end()); return Rcpp::DataFrame::create( Rcpp::Named("id") = x, Rcpp::Named("vals") = xx ); ' f <- cxxfunction(,src,plugin="Rcpp") f()
実行結果は↓。ちゃんと代入されている。
> f() id vals.1 vals.2 vals.3 vals.4 vals.5 1 111 0 111 0 0 0 2 111 0 111 0 0 0
Rcpp+inlineのインストールは