Skip to content
Prev 2286 / 10988 Next

[Rcpp-devel] R.e. Rcpp equivalent of sample()?

In the quickref (and introduction) vignette, there's an example of how
to import a function from R and use it in C++.
Honestly, I've found it easiest to just import sample() from R (in the
stats namespace).  Two things to note:

1.  Performance -- there's a penalty for going back to R, but it's per
function call. So, you can pull a vector result of sample into an
integer vector.

2.  I find it easiest to sample indices rather then elements of the
set that I want to sample, and then do indexing within C++, for
example with an STL style loop.

I'm on the road now, but I can try to fire off an example in the next day or 2.
-Christian