How to resize a NumericVector?

谁说我不能喝 提交于 2019-12-03 11:10:27

If you prefer the C++ idioms, use std::vector<double> and return that at the end where it will be converted via an implicit wrap() to an R vector. You could also use Armadillo or Eigen vectors via RcppArmadillo and RcppEigen.

Our objects are shallow wrappers around the R object, so push_back on, say, a Rcp::NumericVector always needs a full copy. That is known and documented.

Edit: So for completeness, here is an example using RcppArmadillo:

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
arma::vec shrink(arma::vec x) {
    arma::vec y = x;
    y.resize( y.size()-2 );
    return y;
}

which we can deploy via

R> Rcpp::sourceCpp('/tmp/vec.cpp')
R> shrink(1:10)
     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5
[6,]    6
[7,]    7
[8,]    8
R> 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!