[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  which.min: Where is the Min() or Max() or first TRUE or FALSE ?

##  Description

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

##  Usage

    
    
    which.min(x)
    which.max(x)

##  Arguments

x

numeric (logical, integer or double) vector or an  R  object for which the
internal coercion to ` [ double ](/link/double?package=base&version=3.6.2) `
works whose ` [ min ](/link/min?package=base&version=3.6.2) ` or ` [ max
](/link/max?package=base&version=3.6.2) ` is searched for.

##  Value

Missing and ` NaN ` values are discarded.

an ` [ integer ](/link/integer?package=base&version=3.6.2) ` or on 64-bit
platforms, if ` [ length ](/link/length?package=base&version=3.6.2) (x) =: n `
\\(\ge 2^{31}\\) an integer valued ` [ double
](/link/double?package=base&version=3.6.2) ` of length 1 or 0 (iff ` x ` has
no non- ` NA ` s), giving the index of the _first_ minimum or maximum
respectively of ` x ` .

If this extremum is unique (or empty), the results are the same as (but more
efficient than) ` which(x == min(x, na.rm = TRUE)) ` or ` which(x == max(x,
na.rm = TRUE)) ` respectively.

##  Logical <code>x</code> \-- First <code>TRUE</code> or <code>FALSE</code>

For a ` [ logical ](/link/logical?package=base&version=3.6.2) ` vector ` x `
with both ` FALSE ` and ` TRUE ` values, ` which.min(x) ` and ` which.max(x) `
return the index of the first ` FALSE ` or ` TRUE ` , respectively, as ` FALSE
< TRUE ` . However, ` match(FALSE, x) ` or ` match(TRUE, x) ` are typically
_preferred_ , as they do indicate mismatches.

##  See Also

` [ which ](/link/which?package=base&version=3.6.2) ` , ` [ max.col
](/link/max.col?package=base&version=3.6.2) ` , ` [ max
](/link/max?package=base&version=3.6.2) ` , etc.

Use ` [ arrayInd ](/link/arrayInd?package=base&version=3.6.2) () ` , if you
need array/matrix indices instead of 1D vector ones.

` [ which.is.max ](/link/which.is.max?package=base&version=3.6.2&to=nnet) ` in
package nnet differs in breaking ties at random (and having a ‘fuzz’ in the
definition of ties).

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fwhich.min&utm_source=r-docs&utm_medium=docs&utm_term=which.min&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    x <- 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 <- rpois(10000, lambda = 10); x[sample.int(50, 20)] <- NA
    ## where is the first value >= 20 ?
    which.max(x >= 20)
    
    ## Also works for lists (which can be coerced to numeric vectors):
    which.min(list(A = 7, pi = pi)) ##  ->  c(pi = 2L)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fwhich.min&utm_source=r-docs&utm_medium=docs&utm_term=which.min&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)