10
I'm looking into using some C++ for simple parts of my R package using the Rcpp package. I'm a C++ novice (but keen to learn!). I've implemented a few simple cpp programs using the excellent Rcpp - in fact that package has motivated me to learn C++...
Anyway, I've got stuck with a simple problem, which if I can fix would help lots. I have a NumericVector I want to subset and then sort. The code below sorts the whole vector (and would also deal with NAs, which is what I need).
My question is, say I want to extract a part of this vector, sort and have it available for other processing - how can I do that? For example, for a vector of length 10, how do I extract and sort the elements 5:10?
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
RcppExport SEXP rollP(SEXP x) {
NumericVector A(x); // the data
A = sort_unique(A);
return A;
}
which I call from R:
sourceCpp( "rollP.cpp")
rollP(10:1)
# [1] 1 2 3 4 5 6 7 8 9 10