Loading...

Subsetting NumericVector in Rcpp with multiple index ranges


I wish to subset a NumericVector in Rcpp using multiple index ranges. In R I would do this the following way:

b <- as.vector(seq(1:100))
c <- b[c(1:3,5:7,9:14)]

In Rcpp I have got the following to return values from a single set of range values:

cppFunction('NumericVector sub_vec(NumericVector my_vec, int low, int high) {
  return my_vec[my_vec > low & my_vec < high];
}')
sub_vec(b,1,3)

How can I modify this function to include multiple ranges like those shown in the above R example?

Any help much appreciated.

- - Source
comments powered by Disqus