[Rcpp-devel] R.e. Rcpp equivalent of sample()?
Christian Gunning
xian at unm.edu
Sun May 15 02:41:49 CEST 2011
> In the Rcpp vignettes, I found some of the R-like functions for
> sampling from distributions (runif, rnorm, etc), but I didn't see a function
> mimicking sample(). I checked the list of unit tests and didn't see it there
> either. Have I missed it? Is there a C++ function I should be using to
> sample (with replacement in my particular situation) from a vector (with
> provided weights).
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
--
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
More information about the Rcpp-devel
mailing list