
Last chance! 50% off unlimited learning
Sale ends in
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.
expandGridSample(
..., n = NULL, sampleVec = NULL, seed = NULL,
nThreads = NULL, namedSample = FALSE, return_df = FALSE
)
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
.
vectors, factors or a list containing these. (See ?expand.grid
).
Number of results to return. The default is NULL
.
A vector of numbers representing the lexicographical partition of groups to return. Accepts vectors of class bigz
as well as vectors of characters
Random seed initialization. The default is NULL
. N.B. If the gmp library is needed, this parameter must be set in order to have reproducible results (E.g set.seed()
has no effect in these cases).
Specific number of threads to be used. The default is NULL
.
Logical flag. If TRUE
, rownames
corresponding to the lexicographical result, will be added to the returned matrix. The default is FALSE
.
Logical flag to force the output to be a data.frame
. The default is FALSE
.
Joseph Wood
These algorithms rely on efficiently generating the
## 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
Run the code above in your browser using DataLab