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

View source: R/fastcombn.R

fastcombnR Documentation

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

combn

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 documentation built on Oct. 22, 2024, 5:06 p.m.