Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example to subset a vector using an index range #119

Open
aryoda opened this issue Nov 18, 2019 · 0 comments
Open

Add example to subset a vector using an index range #119

aryoda opened this issue Nov 18, 2019 · 0 comments

Comments

@aryoda
Copy link

aryoda commented Nov 18, 2019

The gallery has a page on subsetting:

One "trivial" example is missing: Subsetting with an index range (like R does with x[10:20]).

There is an answer for that at SO which also strives for good performance by avoiding object copies:

https://stackoverflow.com/questions/46668257/rcpp-subsetting-contiguous-stringvector

This code snippet code be the base for extending the subsetting article in the gallery:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::StringVector in_range(Rcpp::StringVector &x, int i, int j) {
  return x[Rcpp::Range(i - 1, j - 1)]; // zero indexed
}

Edit: I guess int i and int j should be replaced by R_xlen_t...

Edit 2: i and j are also not validated against the vector size...

@aryoda aryoda changed the title Add example: Subset a vector using an index range Add example to subset a vector using an index range Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant