\Header{quantile}{Sample Quantiles}
\alias{quantile.default}{quantile}
\keyword{univar}{quantile}
\begin{Usage}
\begin{verbatim}
quantile(x, probs=seq(0, 1, 0.25), na.rm=FALSE, names = TRUE)
\end{verbatim}
\end{Usage}
\begin{Description}
The generic function \texttt{quantile} produces sample quantiles
corresponding to the given probabilities.
The smallest observation corresponds to a probability of 0 and the
largest to a probability of 1.\end{Description}
\begin{Details}
A vector of length \texttt{length(probs)} is returned;
if \texttt{names = TRUE}, it has a \texttt{\Link{names}} attribute.

\texttt{quantile(x,p)} as a function of \texttt{p} linearly interpolates
the points ( (i-1)/(n-1), ox[i] ), where
\texttt{ox <- order(x)} (the ``order statistics'') and \texttt{n <- length(x)}.

This gives \texttt{quantile(x, p) == (1-f)*ox[i] + f*ox[i+1]}, where
\texttt{r <- 1 + (n-1)*p}, \texttt{i <- floor(r)}, \texttt{f <- r - i}
\emph{and} \texttt{ox[n+1] :=  ox[n]}.\end{Details}
\begin{Examples}
\begin{ExampleCode}
quantile(x <- rnorm(1001))# Extremes & Quartiles by default
quantile(x,  probs=c(.1,.5,1,2,5,10,50)/100)

n <- length(x) ## the following is exact, because 1/(1001-1) is exact:
all(abs(sort(x) == quantile(x, probs = ((1:n)-1)/(n-1), names=F)))# TRUE

n <- 777
ox <- sort(x <- round(rnorm(n),1))# round() produces ties
ox <- c(ox, ox[n]) #- such that ox[n+1] := ox[n]
p <- c(0,1,runif(100))
i <- floor(r <- 1 + (n-1)*p)
f <- r - i
all(abs(quantile(x,p) - ((1-f)*ox[i] + f*ox[i+1])) < 20*.Machine$double.eps)
\end{ExampleCode}
\end{Examples}

