Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

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 ‘NULL’ means the identity, i.e., to return the combination (vector of length ‘m’).

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'.

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.

Value

A matrix or a list.

Author(s)

Søren Højsgaard

See Also

Examples

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
  )
}

gRbase

A Package for Graphical Modelling in R

v1.8-6.7
GPL (>= 2)
Authors
Søren Højsgaard <sorenh@math.aau.dk>
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.