<!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: Where is the Min() or Max() or first TRUE or FALSE ?</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 which.min {base}"><tr><td>which.min {base}</td><td style="text-align: right;">R Documentation</td></tr></table>

<h2>Where is the Min() or Max() or first TRUE or FALSE ?</h2>

<h3>Description</h3>

<p>Determines the location, i.e., index of the (first) minimum or maximum
of a numeric (or logical) vector.
</p>


<h3>Usage</h3>

<pre>
which.min(x)
which.max(x)
</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>x</code></td>
<td>
<p>numeric (logical, integer or double) vector or an <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> object
for which the internal coercion to <code><a href="double.html">double</a></code> works whose
<code><a href="Extremes.html">min</a></code> or <code><a href="Extremes.html">max</a></code> is searched for.</p>
</td></tr>
</table>


<h3>Value</h3>

<p>Missing and <code>NaN</code> values are discarded.
</p>
<p>an <code><a href="integer.html">integer</a></code> or on 64-bit platforms, if
<code><a href="length.html">length</a>(x) =: n</code><i>&gt;= 2^31</i> an integer
valued <code><a href="double.html">double</a></code> of length 1 or 0 (iff <code>x</code> has no
non-<code>NA</code>s), giving the index of the <em>first</em> minimum or
maximum respectively of <code>x</code>.
</p>
<p>If this extremum is unique (or empty), the results are the same as
(but more efficient than) <code>which(x == min(x, na.rm = TRUE))</code> or
<code>which(x == max(x, na.rm = TRUE))</code> respectively.
</p>


<h3>Logical <code>x</code> &ndash; First <code>TRUE</code> or <code>FALSE</code></h3>

<p>For a <code><a href="logical.html">logical</a></code> vector <code>x</code> with both <code>FALSE</code> and
<code>TRUE</code> values, <code>which.min(x)</code> and <code>which.max(x)</code> return
the index of the first <code>FALSE</code> or <code>TRUE</code>, respectively, as
<code>FALSE &lt; TRUE</code>.  However, <code>match(FALSE, x)</code> or
<code>match(TRUE, x)</code> are typically <em>preferred</em>, as they do
indicate mismatches.
</p>


<h3>Author(s)</h3>

<p>Martin Maechler</p>


<h3>See Also</h3>

<p><code><a href="which.html">which</a></code>, <code><a href="maxCol.html">max.col</a></code>, <code><a href="Extremes.html">max</a></code>, etc.
</p>
<p>Use <code><a href="which.html">arrayInd</a>()</code>, if you need array/matrix indices instead
of 1D vector ones.
</p>
<p><code><a href="../../nnet/html/which.is.max.html">which.is.max</a></code> in package <a href="https://CRAN.R-project.org/package=nnet"><span class="pkg">nnet</span></a> differs in
breaking ties at random (and having a &lsquo;fuzz&rsquo; in the definition
of ties).
</p>


<h3>Examples</h3>

<pre>
x &lt;- c(1:4, 0:5, 11)
which.min(x)
which.max(x)

## it *does* work with NA's present, by discarding them:
presidents[1:30]
range(presidents, na.rm = TRUE)
which.min(presidents) # 28
which.max(presidents) #  2

## Find the first occurrence, i.e. the first TRUE, if there is at least one:
x &lt;- rpois(10000, lambda = 10); x[sample.int(50, 20)] &lt;- NA
## where is the first value &gt;= 20 ?
which.max(x &gt;= 20)

## Also works for lists (which can be coerced to numeric vectors):
which.min(list(A = 7, pi = pi)) ##  -&gt;  c(pi = 2L)

</pre>

<hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.1 <a href="00Index.html">Index</a>]</div>
</body></html>
