comboGroupsSample {RcppAlgos} | R Documentation |
Sample Partitions of a Vector into Groups
Description
Generate a specific (lexicographically) or random sample of partitions of groups.
Produce results in parallel using the
Parallel
ornThreads
arguments.GMP support allows for exploration where the number of results is large.
Usage
comboGroupsSample(v, numGroups = NULL, grpSizes = NULL, retType = "matrix",
n = NULL, sampleVec = NULL, seed = NULL, Parallel = FALSE,
nThreads = NULL, namedSample = FALSE)
Arguments
v |
Source vector. If |
numGroups |
An Integer. The number of groups that the vector will be partitioned into. The default is |
grpSizes |
A vector of whole numbers representing the size of each group. The default is |
retType |
A string, "3Darray" or "matrix", that determines the shape of the output. The default is "matrix". Note, "3Darray" can only be used when the size of each group is uniform. When the size of each group varies, the return output will always be a matrix. |
n |
Number of results to return. The default is |
sampleVec |
A vector of numbers representing the lexicographical partition of groups to return. Accepts vectors of class |
seed |
Random seed initialization. The default is |
Parallel |
Logical value indicating whether results should be generated in parallel. The default is |
nThreads |
Specific number of threads to be used. The default is |
namedSample |
Logical flag. If |
Details
These algorithms rely on efficiently generating the n^{th}
lexicographical result.
Value
By default, a matrix is returned with column names corresponding to the associated group. If retType = "3Darray"
, a 3D array is returned.
Author(s)
Joseph Wood
References
Examples
## generate 10 random partitions of groups of equal size
comboGroupsSample(10, 2, n = 10, seed = 123)
## generate 10 random partitions of groups of varying sizes
comboGroupsSample(10, grpSizes = 1:4, n = 10, seed = 123)
## using sampleVec to generate specific results
comboGroupsSample(15, 5, sampleVec = c(1, 100, 1e3, 1e6))
all.equal(comboGroupsSample(10, 5,
sampleVec = 1:comboGroupsCount(10, 5)),
comboGroups(10, 5))
## Examples with enormous number of total results
num = comboGroupsCount(100, 20)
gmp::log2.bigz(num)
## [1] 325.5498
first = gmp::urand.bigz(n = 1, size = 325, seed = 123)
mySamp = do.call(c, lapply(0:10, function(x) gmp::add.bigz(first, x)))
class(mySamp)
## [1] "bigz"
## using the sampling function
cbgSamp = comboGroupsSample(100, 20, sampleVec = mySamp)
## using the standard function
cbgGeneral = comboGroups(100, 20,
lower = first,
upper = gmp::add.bigz(first, 10))
identical(cbgSamp, cbgGeneral)
## [1] TRUE
## Not run:
## Using Parallel
system.time(comboGroupsSample(1000, 20, n = 80, seed = 10, Parallel = TRUE))
## End(Not run)