Message-ID: <BANLkTimPbfzisi9yg0+s7MtjF7YkrbTCMw@mail.gmail.com>
Date: 2011-05-15T00:41:49Z
From: Christian Gunning
Subject: [Rcpp-devel] R.e. Rcpp equivalent of sample()?
> 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!