\HeaderA{rowMedians}{Calculates the median for each row in a matrix}{rowMedians}
\aliasA{rowMedians,ExpressionSet-method}{rowMedians}{rowMedians,ExpressionSet.Rdash.method}
\aliasA{rowMedians,exprSet-method}{rowMedians}{rowMedians,exprSet.Rdash.method}
\aliasA{rowMedians,matrix-method}{rowMedians}{rowMedians,matrix.Rdash.method}
\keyword{manip}{rowMedians}
\begin{Description}\relax
Calculates the median for each row in a matrix.
\end{Description}
\begin{Usage}
\begin{verbatim}rowMedians(imat, na.rm=FALSE)\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{imat}] A \code{\LinkA{numeric}{numeric}} \code{\LinkA{matrix}{matrix}}.
\item[\code{na.rm}] If \code{\LinkA{TRUE}{TRUE}}, \code{\LinkA{NA}{NA}}s are excluded before calculating the medians, otherwise not.
\item[\code{...}] Not use.
\end{ldescription}
\end{Arguments}
\begin{Value}
Returns a \code{\LinkA{double}{double}} \code{\LinkA{vector}{vector}} of length equal to number of rows in \code{x}.
\end{Value}
\begin{Section}{Missing values}
Missing values are excluded before calculating the medians.
\end{Section}
\begin{Section}{Benchmarking}
This implementation is optimized for speed and memory to calculate.
As the example shows, this implementation is roughly 3-10 times faster
than using \code{apply(x, MARGIN=1, FUN=medians)}.
As the example might show, the \code{\LinkA{rowQ}{rowQ}()} does not (have to)
handle missing values, and is therefore in some cases faster.
\end{Section}
\begin{Author}\relax
Henrik Bengtsson
\end{Author}
\begin{SeeAlso}\relax
See \code{rowMeans()} in \code{\LinkA{colSums}{colSums}}().
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
set.seed(1)
x <- rnorm(n=234*543)
x[sample(1:length(x), size=0.1*length(x))] <- NA
dim(x) <- c(234,543)
y1 <- rowMedians(x, na.rm=TRUE)
y2 <- apply(x, MARGIN=1, FUN=median, na.rm=TRUE)
stopifnot(all.equal(y1, y2))

x <- cbind(x1=3, x2=c(4:1, 2:5))
stopifnot(all.equal(rowMeans(x), rowMedians(x)))
\end{ExampleCode}
\end{Examples}

