Learn R Programming

RcppAlgos (version 2.3.2)

comboSample: Sample combinations/permutations

Description

  • Generate a specific (lexicographically) or random sample of combinations/permutations.

  • Produce results in parallel using the Parallel or nThreads arguments.

  • GMP support allows for exploration of combinations/permutations of vectors with many elements.

Usage

comboSample(v, m = NULL, repetition = FALSE, freqs = NULL, 
             n = NULL, sampleVec = NULL, seed = NULL, FUN = NULL,
              Parallel = FALSE, nThreads = NULL)

permuteSample(v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL, sampleVec = NULL, seed = NULL, FUN = NULL, Parallel = FALSE, nThreads = NULL)

Arguments

v

Source vector. If v is an integer, it will be converted to the sequence 1:v.

m

Number of elements to choose. If repetition = TRUE, m can exceed the length of v. The default is NULL.

repetition

Logical value indicating whether combinations/permutations should be with or without repetition. The default is FALSE.

freqs

A vector of frequencies used for producing all combinations/permutations of a multiset of v. Each element of freqs represents how many times each element of the source vector, v, is repeated. It is analogous to the times argument in rep. The default value is NULL.

n

Number of combinations/permutations to return. The default is NULL.

sampleVec

A vector of numbers representing the lexicographical combination/permutations to return. Accepts vectors of class bigz as well as vectors of characters

seed

Random seed initialization. The default is NULL.

FUN

Function to be applied to each combination/permutation. The default is NULL.

Parallel

Logical value indicating whether combinations/permutations should be generated in parallel. The default is FALSE. If TRUE and nThreads = NULL, the number of threads used is equal to the minimum of one minus the number of threads available on your system and the number of results requested (e.g. if user has 16 threads and only needs 5 results, 5 threads will be used (i.e. min(16 - 1, 5) = 5)). If nThreads is not NULL, it will be given preference (e.g. if user has 8 threads with Parallel = TRUE and nThreads = 4, only 4 threads will be spawned). If your system is single-threaded, the arguments Parallel and nThreads are ignored.

nThreads

Specific number of threads to be used. The default is NULL. See Parallel.

Value

In general, a matrix is returned with each row containing a vector of length \(m\). If m isn't supplied and freqs is given, a matrix is returned with each row containing a vector of length sum(freqs). If FUN is utilized, a list is returned.

Details

These algorithms rely on efficiently generating the \(n^{th}\) lexicographical combination/permutation (sometimes called the rank).

References

Lexicographical order

Examples

Run this code
# NOT RUN {
## generate 10 random combinations
comboSample(30, 15, TRUE, n = 10, seed = 10)

## using sampleVec to generate specific permutations
permuteSample(15, 10, freqs = c(1,2,2,1,2,2,1,2,1,2,2,1,2,1,1), 
              sampleVec = c(1, 10^2, 10^5, 10^8, 10^11))
              
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)
## [1] 265.7268

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)
## [1] "bigz"

## 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)
## [1] TRUE

## Using nThreads
permuteSample(10000, 50, n = 8, seed = 10, nThreads = 2)

## Using FUN
permuteSample(10000, 50, n = 8, seed = 10, FUN = sd)

# }
# NOT RUN {
## Using Parallel
permuteSample(10000, 50, n = 80, seed = 10, Parallel = TRUE)
# }

Run the code above in your browser using DataLab