EVOLUTION-MANAGER
Edit File: combn.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Generate All Combinations of n Elements, Taken m at a Time</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for combn {utils}"><tr><td>combn {utils}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Generate All Combinations of n Elements, Taken m at a Time</h2> <h3>Description</h3> <p>Generate all combinations of the elements of <code>x</code> taken <code>m</code> at a time. If <code>x</code> is a positive integer, returns all combinations of the elements of <code>seq(x)</code> taken <code>m</code> at a time. If argument <code>FUN</code> is not <code>NULL</code>, applies a function given by the argument to each point. If simplify is FALSE, returns a list; otherwise returns an <code><a href="../../base/html/array.html">array</a></code>, typically a <code><a href="../../base/html/matrix.html">matrix</a></code>. <code>...</code> are passed unchanged to the <code>FUN</code> function, if specified. </p> <h3>Usage</h3> <pre> combn(x, m, FUN = NULL, simplify = TRUE, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>vector source for combinations, or integer <code>n</code> for <code>x <- <a href="../../base/html/seq.html">seq_len</a>(n)</code>.</p> </td></tr> <tr valign="top"><td><code>m</code></td> <td> <p>number of elements to choose.</p> </td></tr> <tr valign="top"><td><code>FUN</code></td> <td> <p>function to be applied to each combination; default <code>NULL</code> means the identity, i.e., to return the combination (vector of length <code>m</code>).</p> </td></tr> <tr valign="top"><td><code>simplify</code></td> <td> <p>logical indicating if the result should be simplified to an <code><a href="../../base/html/array.html">array</a></code> (typically a <code><a href="../../base/html/matrix.html">matrix</a></code>); if FALSE, the function returns a <code><a href="../../base/html/list.html">list</a></code>. Note that when <code>simplify = TRUE</code> as by default, the dimension of the result is simply determined from <code>FUN(<var>1st combination</var>)</code> (for efficiency reasons). This will badly fail if <code>FUN(u)</code> is not of constant length.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>optionally, further arguments to <code>FUN</code>.</p> </td></tr> </table> <h3>Details</h3> <p>Factors <code>x</code> are accepted. </p> <h3>Value</h3> <p>A <code><a href="../../base/html/list.html">list</a></code> or <code><a href="../../base/html/array.html">array</a></code>, see the <code>simplify</code> argument above. In the latter case, the identity <code>dim(combn(n, m)) == c(m, choose(n, m))</code> holds. </p> <h3>Author(s)</h3> <p>Scott Chasalow wrote the original in 1994 for S; R package <a href="https://CRAN.R-project.org/package=combinat"><span class="pkg">combinat</span></a> and documentation by Vince Carey <a href="mailto:stvjc@channing.harvard.edu">stvjc@channing.harvard.edu</a>; small changes by the R core team, notably to return an array in all cases of <code>simplify = TRUE</code>, e.g., for <code>combn(5,5)</code>. </p> <h3>References</h3> <p>Nijenhuis, A. and Wilf, H.S. (1978) <em>Combinatorial Algorithms for Computers and Calculators</em>; Academic Press, NY. </p> <h3>See Also</h3> <p><code><a href="../../base/html/Special.html">choose</a></code> for fast computation of the <em>number</em> of combinations. <code><a href="../../base/html/expand.grid.html">expand.grid</a></code> for creating a data frame from all combinations of factors or vectors. </p> <h3>Examples</h3> <pre> 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))) </pre> <hr /><div style="text-align: center;">[Package <em>utils</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>