combinations.html 3.18 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>R: Enumerate the Combinations or Permutations of the Elements of a Vector</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../R.css">
</head><body>

<table width="100%" summary="page for combinations {gtools}"><tr><td>combinations {gtools}</td><td align="right">R Documentation</td></tr></table>
<h2>Enumerate the Combinations or Permutations of the Elements of a Vector</h2>


<h3>Description</h3>

<p>
<code>combinations</code> enumerates the possible combinations of a
specified size from the elements of a vector.  <code>permutations</code>
enumerates the possible permutations.
</p>


<h3>Usage</h3>

<pre>
combinations(n, r, v=1:n, set=TRUE, repeats.allowed=FALSE)
permutations(n, r, v=1:n, set=TRUE, repeats.allowed=FALSE)
</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>n</code></td>
<td>
Size of the source vector </td></tr>
<tr valign="top"><td><code>r</code></td>
<td>
Size of the target vectors </td></tr>
<tr valign="top"><td><code>v</code></td>
<td>
Source vector. Defaults to <code>1:n</code></td></tr>
<tr valign="top"><td><code>set</code></td>
<td>
Logical flag indicating whether duplicates should be
removed from the source vector <code>v</code>. Defaults to <code>TRUE</code>.</td></tr>
<tr valign="top"><td><code>repeats.allowed</code></td>
<td>
Logical flag indicating whether the
constructed vectors may include duplicated values.  Defaults to
<code>FALSE</code>.  </td></tr>
</table>

<h3>Details</h3>

<p>
Caution: The number of combinations and permutations increases rapidly
with <code>n</code> and <code>r</code>!.
</p>
<p>
To use values of <code>n</code> above about 45, you will need to increase
R's recursion limit.  See the <code>expression</code> argument to the
<code>options</code> command for details on how to do this.
</p>


<h3>Value</h3>

<p>
Returns a matrix where each row contains a vector of length <code>r</code>.</p>

<h3>Author(s)</h3>

<p>
Original versions by Bill Venables
<a href="mailto:Bill.Venables@cmis.csiro.au">Bill.Venables@cmis.csiro.au</a>.  Extended to handle
<code>repeats.allowed</code> by Gregory R. Warnes
<a href="mailto:warnes@bst.rochester.edu">warnes@bst.rochester.edu</a>.
</p>


<h3>References</h3>

<p>
Venables, Bill.  "Programmers Note", R-News, Vol 1/1,
Jan. 2001. <a href="http://cran.r-project.org/doc/Rnews">http://cran.r-project.org/doc/Rnews</a>
</p>


<h3>See Also</h3>

<p>
<code><a href="../../base/html/choose.html">choose</a></code>, <code><a href="../../base/html/options.html">options</a></code>
</p>


<h3>Examples</h3>

<pre>
combinations(3,2,letters[1:3])
combinations(3,2,letters[1:3],repeats=TRUE)

permutations(3,2,letters[1:3])
permutations(3,2,letters[1:3],repeats=TRUE)

# To use large 'n', you need to change the default recusion limit
options(expressions=1e5)
cmat &lt;- combinations(300,2)
dim(cmat) # 44850 by 2 
</pre>



<hr><div align="center">[Package <em>gtools</em> version 2.4.0 <a href="00Index.html">Index]</a></div>

</body></html>