MCPcopy Create free account
hub / github.com/RcppCore/Rcpp / sample

Function sample

inst/include/Rcpp/sugar/functions/sample.h:400–433  ·  view source on GitHub ↗

Adapted from `do_sample`

Source from the content-addressed store, hash-verified

398
399// Adapted from `do_sample`
400inline Vector<INTSXP>
401sample(int n, int size, bool replace = false, sugar::probs_t probs = R_NilValue, bool one_based = true)
402{
403 if (probs.isNotNull()) {
404 Vector<REALSXP> p = clone(probs.get());
405 if (static_cast<int>(p.size()) != n) {
406 stop("probs.size() != n!");
407 }
408
409 sugar::Normalize(p, size, replace);
410
411 if (replace) {
412 int i = 0, nc = 0;
413 for ( ; i < n; i++) {
414 nc += (n * p[i] > 0.1);
415 }
416
417 return nc > 200 ? sugar::WalkerSample(p, n, size, one_based) :
418 sugar::SampleReplace(p, n, size, one_based);
419 }
420
421 if (size > n) {
422 stop("Sample size must be <= n when not using replacement!");
423 }
424
425 return sugar::SampleNoReplace(p, n, size, one_based);
426 }
427
428 if (!replace && size > n) {
429 stop("Sample size must be <= n when not using replacement!");
430 }
431
432 return sugar::EmpiricalSample(n, size, replace, one_based);
433}
434
435template <int RTYPE>
436inline Vector<RTYPE>

Callers 7

sample_dot_intFunction · 0.85
sample_intFunction · 0.85
sample_dblFunction · 0.85
sample_chrFunction · 0.85
sample_cxFunction · 0.85
sample_lglFunction · 0.85
sample_listFunction · 0.85

Calls 10

cloneFunction · 0.85
stopFunction · 0.85
NormalizeFunction · 0.85
WalkerSampleFunction · 0.85
SampleReplaceFunction · 0.85
SampleNoReplaceFunction · 0.85
EmpiricalSampleFunction · 0.85
isNotNullMethod · 0.80
getMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected