Generate combinations of m elements out of x

DESCRIPTION:

Generate all combinations of m elements out of x or 1:n, and optionally apply a function to each, and return a list or simplify to a matrix or array.

USAGE:

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

REQUIRED ARGUMENTS:

x
This is normally an integer n, in which case combinations of elements from 1:n are returned. Otherwise this is a a vector, and elements of the vector are returned.
m
Number of elements in each combination.

OPTIONAL ARGUMENTS:

FUN
function to apply to each combination. If NULL, the combinations themselves are returned, one in each column of the result.
simplify
If FALSE, the results are returned as a list, one combination or function value in each element. If TRUE, if possible the results are simplified to a matrix or array.
...
Optional argument to pass to FUN.

VALUE:

Normally, a matrix with m rows and choose(n,m) columns. If simplify=FALSE a list is returned instead, with choose(n,m) elements. If FUN is supplied then the results of calling the function replace the combinations.

DETAILS:

The case where FUN is supplied are handled by calling , so ... should not include argument to -- in particular avoid X and FUN.

NOTE:

This is similar to the R function combn. However, this version returns a list if simplification is not possible, e.g. because the FUN returns objects with varying lengths.

SEE ALSO:

is used to create the combinations. It is faster to call function directly, note that it gives the combinations in a different order.

is used when is supplied.

EXAMPLES:

combn(5, 3) 
combinations(5, 3) 
combn(letters[1:5], 3) 
combn(5, 3, simplify = FALSE) 
combn(5, 3, FUN = max)