How to slice Rcpp NumericVector for elements 2 to 101?

跟風遠走 提交于 2019-12-04 14:16:44

This is possible with Rcpp's Range function. This generates the equivalent C++ positional index sequence. e.g.

Rcpp::Range(0, 3)

would give:

0 1 2 3

Note: C++ indices begin at 0 not 1!

Example:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::NumericVector subset_range(Rcpp::NumericVector x,
                                 int start = 1, int end = 100) {

  // Use the Range function to create a positional index sequence
  return x[Rcpp::Range(start, end)];
}

/***R
x = rnorm(101)

# Note: C++ indices start at 0 not 1!
all.equal(x[2:101], subset_range(x, 1, 100))
*/
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!