Learn R Programming

gRbase (version 1.8-6.7)

fastcombn: Generate All Combinations of n Elements Taken m at a Time

Description

Generate all combinations of the elements of x taken m at a time. If x is a positive integer, returns all combinations of the elements of seq(x) taken m at a time.

Usage

fastcombn(x, m, FUN = NULL, simplify = TRUE, ...)

combn_prim(x, m, simplify = TRUE)

Arguments

x

vector source for combinations, or integer n for x <- seq(n).

m

number of elements to choose.

FUN

function to be applied to each combination; default <U+2018>NULL<U+2019> means the identity, i.e., to return the combination (vector of length <U+2018>m<U+2019>).

simplify

logical indicating if the result should be simplified to a matrix; if FALSE, the function returns a list.

...

Further arguments passed on to `FUN`.

Value

A matrix or a list.

Details

* Factors `x` are accepted.

* `combn_prim` is a simplified (but faster) version of the `combn` function. Does nok take the `FUN` argument.

* `fastcombn` is intended to be a faster version of the `combn` function.

See Also

combn

Examples

Run this code
# NOT RUN {
x <- letters[1:5]; m <- 3

fastcombn(x, m)
combn(x, m)
combn_prim(x, m)

x <- letters[1:4]; m <- 3
fastcombn(x, m, simplify=FALSE)
combn(x, m, simplify=FALSE)
combn_prim(x, m, simplify=FALSE)

x <- 1:10; m <- 3
fastcombn(x, m, min)
combn(x, m, min)

x <- factor(letters[1:8]); m <- 5

if (require(microbenchmark)){
  microbenchmark(
    combn(x, m, simplify=FALSE),
    combn_prim(x, m, simplify=FALSE),
    fastcombn(x, m, simplify=FALSE),
    times=50
  )
}

# }

Run the code above in your browser using DataLab