\HeaderA{Extremes}{Maxima and Minima}{Extremes}
\aliasA{max}{Extremes}{max}
\aliasA{min}{Extremes}{min}
\aliasA{pmax}{Extremes}{pmax}
\methaliasA{pmax.int}{Extremes}{pmax.int}
\aliasA{pmin}{Extremes}{pmin}
\methaliasA{pmin.int}{Extremes}{pmin.int}
\keyword{univar}{Extremes}
\keyword{arith}{Extremes}
\begin{Description}\relax
Returns the (parallel) maxima and minima of the input values.
\end{Description}
\begin{Usage}
\begin{verbatim}
max(..., na.rm = FALSE)
min(..., na.rm = FALSE)

pmax(..., na.rm = FALSE)
pmin(..., na.rm = FALSE)

pmax.int(..., na.rm = FALSE)
pmin.int(..., na.rm = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{...}] numeric or character arguments (see Note).
\item[\code{na.rm}] a logical indicating whether missing values should be
removed.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{max} and \code{min} return the maximum or minimum of \emph{all}
the  values present in their arguments, as \code{\LinkA{integer}{integer}} if
all are \code{logical} or \code{integer}, as \code{\LinkA{double}{double}} if
all are numeric, and character otherwise.

If \code{na.rm} is \code{FALSE} an \code{NA} value in any of the
arguments will cause a value of \code{NA} to be returned, otherwise
\code{NA} values are ignored.

The minimum and maximum of a numeric empty set are \code{+Inf} and
\code{-Inf} (in this order!) which ensures \emph{transitivity}, e.g.,
\code{min(x1, min(x2)) == min(x1, x2)}.  For numeric \code{x}
\code{max(x) == -Inf} and \code{min(x) == +Inf}
whenever \code{length(x) == 0} (after removing missing values if
requested).  However, \code{pmax} and \code{pmin} return
\code{NA} if all the parallel elements are \code{NA} even for
\code{na.rm = TRUE}.

\code{pmax} and \code{pmin} take one or more vectors (or matrices) as
arguments and return a single vector giving the \sQuote{parallel}
maxima (or minima) of the vectors.  The first element of the result is
the maximum (minimum) of the first elements of all the arguments, the
second element of the result is the maximum (minimum) of the second
elements of all the arguments and so on.  Shorter inputs are recycled
if necessary.  \code{\LinkA{attributes}{attributes}} (such as \code{\LinkA{names}{names}} or
\code{\LinkA{dim}{dim}}) are transferred from the first argument (if
applicable).

\code{pmax.int} and \code{pmin.int} are faster internal versions only
used when all arguments are atomic vectors and there are no classes:
they drop all attributes.  (Note that all versions fail for raw and
complex vectors since these have no ordered.)

\code{max} and \code{min} are generic functions: methods can be
defined for them individually or via the
\code{\LinkA{Summary}{Summary}} group generic.  For this to
work properly, the arguments \code{...} should be unnamed, and
dispatch is on the first argument.

By definition the min/max of any vector containing an \code{NaN} is
\code{NaN}, except that the min/max of any vector containing an
\code{NA} is \code{NA} even if it also contains an \code{NaN}.  Note
that \code{max(NA, Inf) == NA} even though the maximum would be
\code{Inf} whatever the missing value actually is.

The max/min of an empty character vector is a character \code{NA}.
(One could argue that as \code{""} is the smallest character element,
the maximum should be \code{""}, but there is no obvious candidate for
the minimum.)
\end{Details}
\begin{Value}
For \code{min} or \code{max}, a length-one vector.  For \code{pmin} or
\code{pmax}, a vector of length the longest of the input vectors.

The type of the result will be that of the highest of the inputs in
the hierarchy integer < real < character.

For \code{min} and \code{max} if there are only numeric inputs and all
are empty (after possible removal of \code{NA}s), the result is double
(\code{Inf} or \code{-Inf}).
\end{Value}
\begin{Section}{S4 methods}
\code{max} and \code{min} are part of the S4
\code{\LinkA{Summary}{Summary}} group generic.  Methods
for them must use the signature \code{x, ..., na.rm}.
\end{Section}
\begin{Note}\relax
\sQuote{Numeric} arguments are vectors of type integer and numeric,
and logical (coerced to integer).  For historical reasons, \code{NULL}
is accepted as equivalent to \code{integer(0)}.

\code{pmax} and \code{pmin} will also work on classed objects with
appropriate methods for comparison, \code{is.na} and \code{rep} (if
recycling of arguments is needed).
\end{Note}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{range}{range}} (\emph{both} min and max) and
\code{\LinkA{which.min}{which.min}} (\code{which.max}) for the \emph{arg min},
i.e., the location where an extreme value occurs.

\sQuote{\LinkA{plotmath}{plotmath}} for the use of \code{min} in plot annotation.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
require(stats); require(graphics)
 min(5:1, pi) #-> one number
pmin(5:1, pi) #->  5  numbers

x <- sort(rnorm(100));  cH <- 1.35
pmin(cH, quantile(x)) # no names
pmin(quantile(x), cH) # has names
plot(x, pmin(cH, pmax(-cH, x)), type='b', main= "Huber's function")
\end{ExampleCode}
\end{Examples}

