Description Details Author(s) Examples
Rcpp sugar provides numerous p/q/d/r functions for numerous distributions.
This example shows (in the corresponding C++ code) how to draw from three different distributions and returns a data frame.
The various header file, and the Rcpp sugar vignette, provide full documentation for Rcpp sugar.
The C++ source file corresponding to the this function does the
following (inside of a try/catch
block):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Rcpp::RNGScope scope; // needed when RNGs are drawn
int n = Rcpp::as<int>(ns); // length vector
Rcpp::NumericVector rn = Rcpp::rnorm(n);
Rcpp::NumericVector rt = Rcpp::rt(n, 1.0);
Rcpp::NumericVector rp = Rcpp::rpois(n, 1.0);
// create a new data frame to return drawns
Rcpp::DataFrame NDF =
Rcpp::DataFrame::create(Rcpp::Named("rnorm") =rn,
Rcpp::Named("rt") =rt,
Rcpp::Named("rpois") =rp);
// and return old and new in list
return(NDF);
|
As shown in the example section, provided the seed is reset, the exact same draws can be obtained in R itself – which is important for reproducibility.
Dirk Eddelbuettel and Romain Francois
1 2 3 4 5 |
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.