combn {utils}
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. If argument FUN is not NULL, applies a function given by the argument to each point. If simplify is FALSE, returns a list; otherwise returns an array, typically a matrix. ... are passed unchanged to the FUN function, if specified.
Usage
combn(x, m, FUN = NULL, simplify = TRUE, ...)
Arguments
- x
- vector source for combinations, or integer
nforx <- seq_len(n). - m
- number of elements to choose.
- FUN
- function to be applied to each combination; default
NULLmeans the identity, i.e., to return the combination (vector of lengthm). - simplify
- logical indicating if the result should be simplified to an
array(typically amatrix); if FALSE, the function returns alist. Note that whensimplify = TRUEas by default, the dimension of the result is simply determined fromFUN(<var>1st combination</var>)(for efficiency reasons). This will badly fail ifFUN(u)is not of constant length. - ...
- optionally, further arguments to
FUN.
Values
a list or array, see the simplify argument above. In the latter case, the identity dim(combn(n,m)) == c(m, choose(n,m)) holds.
References
Nijenhuis, A. and Wilf, H.S. (1978) Combinatorial Algorithms for Computers and Calculators; Academic Press, NY.
See Also
choose for fast computation of the number of combinations. expand.grid for creating a data frame from all combinations of factors or vectors.
Examples
combn(letters[1:4], 2) (m <- combn(10, 5, min)) # minimum value in each combination mm <- combn(15, 6, function(x) matrix(x, 2,3)) stopifnot(round(choose(10,5)) == length(m), c(2,3, round(choose(15,6))) == dim(mm)) ## Different way of encoding points: combn(c(1,1,1,1,2,2,2,3,3,4), 3, tabulate, nbins = 4) ## Compute support points and (scaled) probabilities for a ## Multivariate-Hypergeometric(n = 3, N = c(4,3,2,1)) p.f.: # table.mat(t(combn(c(1,1,1,1,2,2,2,3,3,4), 3, tabulate,nbins=4))) ## Assuring the identity for(n in 1:7) for(m in 0:n) stopifnot(is.array(cc <- combn(n, m)), dim(cc) == c(m, choose(n,m)))
Documentation reproduced from R 2.15.0. License: GPL-2.
