Learn R Programming

caTools (version 1.17.1)

combs: All Combinations of k Elements from Vector v

Description

Finds all unordered combinations of k elements from vector v.

Usage

combs(v,k)

Arguments

Value

combs(v,k) (where v has length n) creates a matrix with $\frac{n!}{(n-k)! k!}$ (n choose k) rows and k columns containing all possible combinations of n elements taken k at a time.

See Also

I discovered recently that R packages already have two functions with similar capabilities: combinations from gTools package and Also similar to Matlab's nchoosek function (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nchoosek.html)

Examples

Run this code
combs(2:5, 3) # display examples
  combs(c("cats", "dogs", "mice"), 2)
  
  a = combs(1:4, 2)
  b = matrix( c(1,1,1,2,2,3,2,3,4,3,4,4), 6, 2)
  stopifnot(a==b)

Run the code above in your browser using DataLab