base (version 3.6.2)

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


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





numeric (logical, integer or double) vector or an R object for which the internal coercion to double works whose min or max is searched for.


Missing and NaN values are discarded.

an integer or on 64-bit platforms, if length(x) =: n\(\ge 2^{31}\) an integer valued double of length 1 or 0 (iff x has no non-NAs), 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 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, max.col, max, etc.

Use arrayInd(), if you need array/matrix indices instead of 1D vector ones. in package nnet differs in breaking ties at random (and having a ‘fuzz’ in the definition of ties).


Run this code
x <- c(1:4, 0:5, 11)

## it *does* work with NA's present, by discarding them:
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[, 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 DataLab