combn
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. Return a list, or simplify to a matrix or an array.

Usage

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

Arguments

x normally an integer n, in which case combinations of elements from 1:n are returned. Otherwise x is a vector, and elements of the vector are returned.
m the number of elements in each combination.
FUN the function to apply to each combination. If NULL, the combinations themselves are returned, one in each column of the result.
simplify A logical value. if FALSE, the results are returned as a list, one combination or function value in each element. If TRUE (the default), if possible, the results are simplified to a matrix or an array.
... any optional argument to pass to FUN.
Value
normally returns a matrix with m rows and choose(n,m) columns.
See Also
choose.
Examples
combn(5, 3) 
combn(letters[1:5], 3) 
combn(5, 3, simplify = FALSE) 
combn(5, 3, FUN = max) 
combn(5, 3, FUN = range)
combn(5,3, FUN = diag) # produces an array 

combn(5,3, FUN = diag, simplify = FALSE, nrow = 2) # list of 2x2 matrices with some warnings

Package utils version 6.1.1-7
Package Index