1% File src/library/base/man/which.min.Rd 2% Part of the R package, https://www.R-project.org 3% Copyright 1995-2016 R Core Team 4% Distributed under GPL 2 or later 5 6\name{which.min} 7\alias{which.min} 8\alias{which.max} 9\title{Where is the Min() or Max() or first TRUE or FALSE ?} 10\concept{argmin} 11\concept{argmax} 12\concept{index of minimum} 13\concept{index of maximum} 14\concept{index of first TRUE} 15\concept{index of first FALSE} 16\description{ 17 Determines the location, i.e., index of the (first) minimum or maximum 18 of a numeric (or logical) vector. 19} 20\usage{ 21which.min(x) 22which.max(x) 23} 24\arguments{ 25 \item{x}{numeric (logical, integer or double) vector or an \R object 26 for which the internal coercion to \code{\link{double}} works whose 27 \code{\link{min}} or \code{\link{max}} is searched for.} 28} 29\section{Logical \code{x} -- First \code{TRUE} or \code{FALSE}}{ 30 For a \code{\link{logical}} vector \code{x} with both \code{FALSE} and 31 \code{TRUE} values, \code{which.min(x)} and \code{which.max(x)} return 32 the index of the first \code{FALSE} or \code{TRUE}, respectively, as 33 \code{FALSE < TRUE}. However, \code{match(FALSE, x)} or 34 \code{match(TRUE, x)} are typically \emph{preferred}, as they do 35 indicate mismatches. 36} 37\value{ 38 Missing and \code{NaN} values are discarded. 39 40 an \code{\link{integer}} or on 64-bit platforms, if 41 \code{\link{length}(x) =: n}\eqn{\ge 2^{31}}{>= 2^31} an integer 42 valued \code{\link{double}} of length 1 or 0 (iff \code{x} has no 43 non-\code{NA}s), giving the index of the \emph{first} minimum or 44 maximum respectively of \code{x}. 45 46 If this extremum is unique (or empty), the results are the same as 47 (but more efficient than) \code{which(x == min(x, na.rm = TRUE))} or 48 \code{which(x == max(x, na.rm = TRUE))} respectively. 49} 50\author{Martin Maechler} 51\seealso{ 52 \code{\link{which}}, \code{\link{max.col}}, \code{\link{max}}, etc. 53 54 Use \code{\link{arrayInd}()}, if you need array/matrix indices instead 55 of 1D vector ones. 56 57 \code{\link[nnet]{which.is.max}} in package \CRANpkg{nnet} differs in 58 breaking ties at random (and having a \sQuote{fuzz} in the definition 59 of ties). 60} 61\examples{ 62x <- c(1:4, 0:5, 11) 63which.min(x) 64which.max(x) 65 66## it *does* work with NA's present, by discarding them: 67presidents[1:30] 68range(presidents, na.rm = TRUE) 69which.min(presidents) # 28 70which.max(presidents) # 2 71 72## Find the first occurrence, i.e. the first TRUE, if there is at least one: 73x <- rpois(10000, lambda = 10); x[sample.int(50, 20)] <- NA 74## where is the first value >= 20 ? 75which.max(x >= 20) 76 77## Also works for lists (which can be coerced to numeric vectors): 78which.min(list(A = 7, pi = pi)) ## -> c(pi = 2L) 79\dontshow{stopifnot(identical(which.min(list(A = 7, pi = pi)), c(pi = 2L)))} 80} 81\keyword{utilities} 82