EVOLUTION-MANAGER
Edit File: maxCol.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: Find Maximum Position in Matrix</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 maxCol {base}"><tr><td>maxCol {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Find Maximum Position in Matrix</h2> <h3>Description</h3> <p>Find the maximum position for each row of a matrix, breaking ties at random. </p> <h3>Usage</h3> <pre> max.col(m, ties.method = c("random", "first", "last")) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>m</code></td> <td> <p>numerical matrix</p> </td></tr> <tr valign="top"><td><code>ties.method</code></td> <td> <p>a character string specifying how ties are handled, <code>"random"</code> by default; can be abbreviated; see ‘Details’.</p> </td></tr> </table> <h3>Details</h3> <p>When <code>ties.method = "random"</code>, as per default, ties are broken at random. In this case, the determination of a tie assumes that the entries are probabilities: there is a relative tolerance of <i>1e-5</i>, relative to the largest (in magnitude, omitting infinity) entry in the row. </p> <p>If <code>ties.method = "first"</code>, <code>max.col</code> returns the column number of the <em>first</em> of several maxima in every row, the same as <code><a href="unname.html">unname</a>(<a href="apply.html">apply</a>(m, 1, <a href="which.min.html">which.max</a>))</code>.<br /> Correspondingly, <code>ties.method = "last"</code> returns the <em>last</em> of possibly several indices. </p> <h3>Value</h3> <p>index of a maximal value for each row, an integer vector of length <code>nrow(m)</code>. </p> <h3>References</h3> <p>Venables, W. N. and Ripley, B. D. (2002) <em>Modern Applied Statistics with S.</em> New York: Springer (4th ed). </p> <h3>See Also</h3> <p><code><a href="which.min.html">which.max</a></code> for vectors. </p> <h3>Examples</h3> <pre> table(mc <- max.col(swiss)) # mostly "1" and "5", 5 x "2" and once "4" swiss[unique(print(mr <- max.col(t(swiss)))) , ] # 3 33 45 45 33 6 set.seed(1) # reproducible example: (mm <- rbind(x = round(2*stats::runif(12)), y = round(5*stats::runif(12)), z = round(8*stats::runif(12)))) ## Not run: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] x 1 1 1 2 0 2 2 1 1 0 0 0 y 3 2 4 2 4 5 2 4 5 1 3 1 z 2 3 0 3 7 3 4 5 4 1 7 5 ## End(Not run) ## column indices of all row maxima : utils::str(lapply(1:3, function(i) which(mm[i,] == max(mm[i,])))) max.col(mm) ; max.col(mm) # "random" max.col(mm, "first") # -> 4 6 5 max.col(mm, "last") # -> 7 9 11 </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>