Skip to content

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

@aryoda

Description

@aryoda

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions