Learn R Programming

RcppAlgos (version 0.1.0)

comboGeneral: Generate all Combinations of a Vector with/without Constraints

Description

Quickly generate all combinations of a vector, chosen \(m\) at a time, with or without constraints using Rcpp.

Usage

comboGeneral(v, m, repetition = FALSE, constraintFun = NULL,
                comparisonFun = NULL, limitConstraints = NULL, rowCap = 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.

repetition

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

constraintFun

Function to be applied to the elements of v that should be passed as a string (E.g. constraintFun = "sum"). The possible contraint functions are: "sum", "prod", "mean", "max", & "min". The default is NULL, meaning no function is applied.

comparisonFun

Comparison operator that will be used to compare limitConstraints with the result of contraintFun applied to v. Again, it should be passed as a string (E.g. comparisonFun = "<="). The possible comparison operators are: "<", ">", "<=", ">=", "==". The default is NULL.

limitConstraints

This is the value that will be used for comparison. The default is NULL.

rowCap

The maximal number of expected results when a contraint is applied. Can also be used if you only need a specific number of combinations. This is useful when the total number of combinations without contraint is large and you expect/need a small number of combinations that meet the criteria. Using rowCap can drastically improve run time and avoid unnecessary crashes due to lack of memory. See examples below.

Value

Returns a matrix where each row contains a vector of length m.

References

http://gallery.rcpp.org/articles/passing-cpp-function-pointers/

Examples

Run this code
# NOT RUN {
## 8-tuples of 1:17 w/o repetition
system.time(comboGeneral(17,8))

## 10-tuples of 1:13 w/ repetition
system.time(comboGeneral(13,10,repetition = TRUE))

## Generate some random data
set.seed(1009)
mySamp <- rnorm(75, 997, 23)

## How to use rowCap example:
## Researcher only needs 1000 7-tuples of mySamp
## such that the sum is greater than 7200.
system.time(comboGeneral(mySamp,7,FALSE,"sum",">",7200,1000))

## If you leave rowCap as NULL, it can take much longer
## (still fast enough most of the time) and in some cases
## crash your computer as the underlying code allocates
## enough space to account for every combination
## (e.g. In our example, there are choose(75, 7)
## = 1984829850 rows, 7 columns, with each cell occupying
## 8 bytes. This gives a total over 100 GB). 
## (i.e. choose(75, 7)*7*8/(2^30)).

## Find 13-tuple combinations of 1:25 such
## that the mean is less than 10
system.time(myComb <- comboGeneral(25,13,FALSE,"mean","<",10))

# }
# NOT RUN {
## Alternatively, you must generate all combinations and subsequently
## subset to obtain the combinations that meet the criteria
system.time(myComb2 <- combn(25,13))
ystem.time(myCols <- which(apply(myComb2, 2, mean) < 10))
system.time(myComb2 <- myComb2[,myCols])

## Test equality with myComb above
all.equal(myComb,t(myComb2))

## Any variation is much slower
system.time(myComb2 <- combn(25,13)[,combn(25,13,mean) < 10])
# }

Run the code above in your browser using DataLab