[Rcpp-devel] problem with sample() implemented in Rcpp/RcppArmadillo

Rahel Sollmann rasrage at yahoo.de
Sat Oct 27 17:20:13 CEST 2012

Thanks Christian and Dirk, I appreciate your help.
I am using sample() because in my real application probabilities for the members of the vector I want to sample from are not equal. 
I'm happy and embarrassed to say that I figured out my problem. I should have provided a vector with values from 0 to max-1 to sample from, since I use it as an index later. Instead I used 1 to max as you would in R, so whenever max was one of the sampled elements it would result in an error.

 Von: Dirk Eddelbuettel <edd at debian.org>
An: xian at unm.edu 
CC: rcpp-devel at lists.r-forge.r-project.org; Rahel Sollmann <rasrage at yahoo.de> 
Gesendet: 10:50 Samstag, 27.Oktober 2012
Betreff: Re: [Rcpp-devel] problem with sample() implemented in Rcpp/RcppArmadillo

On 27 October 2012 at 05:13, Christian Gunning wrote:
| > I am very new to c++ so I apologize in advance if this is a stupid question:
| I ran into this myself when I started using Rcpp  -- it's a good
| question.  The R API is extensive, but it's vanilla C and doesn't
| handle vectors (and you already know how to sample() a scalar,
| right?).

Someone should sit down and write a C++ variant of sample for Rcpp.  Having a
starting point would help, others can then tune.

Personally, I don't have a need for one so no itch to scratch...

| > For a larger analysis I am trying to implement the R function sample() in c++ via RcppArmadillo.
| There's a non-trivial penalty for reaching back into R for functions
| that aren't exported via the API (e.g. "Rcpp::Function sample =
| base["sample"];").  


| Rcpp Vectors are STL containers, 


We make them look like STL containers by giving them begin(),
end(), ... member functions but they are still R objects ("SEXP") underneath.

That means in particular that the carefully crafted STL algos with their
performance guarantees will NOT have these guarantees.

That said, we can easily wrap genuine STL vectors so the rest of you argument
is good :)

| and many folk have asked the same questions wrt the STL.  There's some good
| background reading here:
| http://stackoverflow.com/questions/6942273/get-random-element-from-container-c-stl

Nice reference, and "+1" for going back to standard STL functions and
behaviour which (in my not so humble opinions) is one of the best approaches
one can undertake when writing C++.


Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121027/335910b7/attachment.html>

More information about the Rcpp-devel mailing list