comboSample {RcppAlgos} | R Documentation |
Sample Combinations and Permutations
Description
Generate a specific (lexicographically) or random sample of combinations/permutations.
Produce results in parallel using the
Parallel
ornThreads
arguments.GMP support allows for exploration of combinations/permutations of vectors with many elements.
Usage
comboSample(v, m = NULL, ...)
permuteSample(v, m = NULL, ...)
## S3 method for class 'numeric'
comboSample(v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL,
sampleVec = NULL, seed = NULL, FUN = NULL, Parallel = FALSE,
nThreads = NULL, namedSample = FALSE, FUN.VALUE = NULL, ...)
## S3 method for class 'numeric'
permuteSample(v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL,
sampleVec = NULL, seed = NULL, FUN = NULL, Parallel = FALSE,
nThreads = NULL, namedSample = FALSE, FUN.VALUE = NULL, ...)
## S3 method for class 'factor'
comboSample(
v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL,
sampleVec = NULL, seed = NULL, FUN = NULL, Parallel = FALSE,
nThreads = NULL, namedSample = FALSE, FUN.VALUE = NULL, ...
)
## S3 method for class 'factor'
permuteSample(
v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL,
sampleVec = NULL, seed = NULL, FUN = NULL, Parallel = FALSE,
nThreads = NULL, namedSample = FALSE, FUN.VALUE = NULL, ...
)
## Default S3 method:
comboSample(
v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL, sampleVec = NULL,
seed = NULL, FUN = NULL, namedSample = FALSE, FUN.VALUE = NULL, ...
)
## Default S3 method:
permuteSample(
v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL, sampleVec = NULL,
seed = NULL, FUN = NULL, namedSample = FALSE, FUN.VALUE = NULL, ...
)
## S3 method for class 'table'
comboSample(
v, m = NULL, n = NULL, sampleVec = NULL, seed = NULL, FUN = NULL,
Parallel = FALSE, nThreads = NULL, namedSample = FALSE, FUN.VALUE = NULL, ...
)
## S3 method for class 'table'
permuteSample(
v, m = NULL, n = NULL, sampleVec = NULL, seed = NULL, FUN = NULL,
Parallel = FALSE, nThreads = NULL, namedSample = FALSE, FUN.VALUE = NULL, ...
)
## S3 method for class 'list'
comboSample(
v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL,
sampleVec = NULL, seed = NULL, namedSample = FALSE, ...
)
## S3 method for class 'list'
permuteSample(
v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL,
sampleVec = NULL, seed = NULL, namedSample = FALSE, ...
)
Arguments
v |
Source vector. If |
m |
Number of elements to choose. If |
... |
Further arguments passed to methods. |
repetition |
Logical value indicating whether combinations/permutations should be with or without repetition. The default is |
freqs |
A vector of frequencies used for producing all combinations/permutations of a multiset of |
n |
Number of combinations/permutations to return. The default is |
sampleVec |
A vector of indices representing the lexicographical combination/permutations to return. Accepts whole numbers as well as vectors of class |
seed |
Random seed initialization. The default is |
FUN |
Function to be applied to each combination/permutation. The default is |
Parallel |
Logical value indicating whether combinations/permutations should be generated in parallel. The default is |
nThreads |
Specific number of threads to be used. The default is |
namedSample |
Logical flag. If |
FUN.VALUE |
A template for the return value from |
Details
These algorithms rely on efficiently generating the n^{th}
lexicographical combination/permutation. This is the process of unranking.
Value
In general, a matrix with
m
orm + 1
columns, depending on the value ofkeepResults
If
FUN
is utilized andFUN.VALUE = NULL
, a list is returnedWhen both
FUN
andFUN.VALUE
are notNULL
, the return is modeled after the return ofvapply
. See the 'Value' section ofvapply
.
Note
-
Parallel
andnThreads
will be ignored in the following cases:If the class of the vector passed is
character
(N.B.Rcpp::CharacterMatrix
is not thread safe). Alternatively, you can generate an indexing matrix in parallel.If
FUN
is utilized.
-
n
andsampleVec
cannot both beNULL
. Factor vectors are accepted. Class and level attributes are preserved except when
FUN
is used.
Author(s)
Joseph Wood
References
See Also
Examples
## generate 10 random combinations
comboSample(30, 8, TRUE, n = 5, seed = 10)
## Using sampleVec to generate specific permutations
fqs = c(1,2,2,1,2,2,1,2,1,2,2,1,2,1,1)
s_idx = c(1, 10^2, 10^5, 10^8, 10^11)
permuteSample(15, 10, freqs = fqs, sampleVec = s_idx)
## Same example using 'table' method
permuteSample(table(rep(1:15, times = fqs)), 10, sampleVec = s_idx)
## Generate each result one by one...
## Same, but not as efficient as generating iteratively
all.equal(comboSample(10, 5, sampleVec = 1:comboCount(10, 5)),
comboGeneral(10, 5))
## Examples with enormous number of total permutations
num = permuteCount(10000, 20)
gmp::log2.bigz(num)
first = gmp::urand.bigz(n = 1, size = 265, seed = 123)
mySamp = do.call(c, lapply(0:10, function(x) gmp::add.bigz(first, x)))
class(mySamp)
## using permuteSample
pSamp = permuteSample(10000, 20, sampleVec = mySamp)
## using permuteGeneral
pGeneral = permuteGeneral(10000, 20,
lower = first,
upper = gmp::add.bigz(first, 10))
identical(pSamp, pGeneral)
## Using nThreads
permPar = permuteSample(10000, 50, n = 8, seed = 10, nThreads = 2)
## Using FUN
permuteSample(10000, 50, n = 4, seed = 10, FUN = sd)
## Not run:
## Using Parallel
permuteSample(10000, 50, n = 80, seed = 10, Parallel = TRUE)
## End(Not run)