expandGridSample {RcppAlgos} | R Documentation |
Sample the Cartesian Product
Description
Generate a specific (lexicographically) or random sample of the Cartesian product of the input vectors.
Produce results in parallel using the
nThreads
arguments.GMP support allows for exploration where the number of results is large.
Usage
expandGridSample(
..., n = NULL, sampleVec = NULL, seed = NULL,
nThreads = NULL, namedSample = FALSE, return_df = FALSE
)
Arguments
... |
vectors, factors or a list containing these. (See |
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 |
nThreads |
Specific number of threads to be used. The default is |
namedSample |
Logical flag. If |
return_df |
Logical flag to force the output to be a |
Details
These algorithms rely on efficiently generating the n^{th}
lexicographical result.
Value
When all of the input is of the same type, by default expandGridSample
produces a matrix
(a data.frame
otherwise). This can be ignored by setting the argument return_df = TRUE
.
Author(s)
Joseph Wood
References
Examples
## input vectors
lst = list(factor(state.abb), euro, islands)
## generate 10 random products
expandGridSample(lst, n = 10, seed = 123)
## using sampleVec to generate specific results
expandGridSample(lst, sampleVec = c(1, 100, 1e3))
all.equal(expandGridSample(lst, sampleVec = 1:expandGridCount(lst)),
expandGrid(lst))
## Examples with enormous number of total results
big_lst = Map(function(x, y) x:y, 8:33, 15:40)
num = expandGridCount(big_lst)
gmp::log2.bigz(num)
## [1] 78
first = gmp::urand.bigz(n = 1, size = 78, seed = 123)
mySamp = do.call(c, lapply(0:10, function(x) gmp::add.bigz(first, x)))
class(mySamp)
## [1] "bigz"
## using the sampling function
cartSamp = expandGridSample(big_lst, sampleVec = mySamp)
## using the standard function
cartGeneral = expandGrid(big_lst,
lower = first,
upper = gmp::add.bigz(first, 10))
identical(cartSamp, cartGeneral)
## [1] TRUE