R: Where is the Min() or Max() or first TRUE or FALSE ?

which.min {base} | R Documentation

## 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.

For a logical vector `x`, `which.min(x)` and `which.max(x)` return the index of the first `FALSE` or `TRUE`, respectively.

### Usage

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

### Arguments

`x` | numeric (integer or double) vector, whose `min` or `max` is searched for.

### Value

Missing and `NaN` values are discarded.

an `integer` 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))` or `which(x == max(x))` respectively.

### Author(s)

Martin Maechler

### See Also

`which`, `max.col`, `max`, etc.

Use `arrayInd()`, if you need array/matrix indices instead of 1D vector ones.

`which.is.max` in package nnet differs in breaking ties at random (and having a ‘fuzz’ in the definition of ties).

### Examples

    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:
    x <- rpois(10000, lambda = 10); x[sample.int(50, 20)] <- NA
    ## where is the first value >= 20 ?
    which.max(x >= 20)

[Package base version 3.1.2 Index]