1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/rowCollapse.R
\name{rowCollapse}
\alias{rowCollapse}
\alias{colCollapse}
\title{Extracts one cell per row (column) from a matrix}
\usage{
rowCollapse(x, idxs, rows = NULL, dim. = dim(x), ..., useNames = TRUE)
colCollapse(x, idxs, cols = NULL, dim. = dim(x), ..., useNames = TRUE)
}
\arguments{
\item{x}{An NxK \code{\link[base]{matrix}} or, if \code{dim.} is specified,
an N * K \code{\link[base]{vector}}.}
\item{idxs}{An index \code{\link[base]{vector}} of (maximum) length N (K)
specifying the columns (rows) to be extracted.}
\item{rows}{A \code{\link[base]{vector}} indicating subset of rows to
operate over. If \code{\link[base]{NULL}}, no subsetting is done.}
\item{dim.}{An \code{\link[base]{integer}} \code{\link[base]{vector}} of
length two specifying the dimension of \code{x}, also when not a
\code{\link[base]{matrix}}. \emph{Comment:} The reason for this argument
being named with a period at the end is purely technical (we get a run-time
error if we try to name it \code{dim}).}
\item{...}{Not used.}
\item{useNames}{If \code{\link[base:logical]{TRUE}} (default), names
attributes of the result are set, otherwise not.}
\item{cols}{A \code{\link[base]{vector}} indicating subset of columns to
operate over. If \code{\link[base]{NULL}}, no subsetting is done.}
}
\value{
Returns a \code{\link[base]{vector}} of length N (K).
}
\description{
Extracts one cell per row (column) from a matrix. The implementation is
optimized for memory and speed.
}
\examples{
x <- matrix(1:27, ncol = 3)
y <- rowCollapse(x, 1)
stopifnot(identical(y, x[, 1]))
y <- rowCollapse(x, 2)
stopifnot(identical(y, x[, 2]))
y <- rowCollapse(x, c(1, 1, 1, 1, 1, 3, 3, 3, 3))
stopifnot(identical(y, c(x[1:5, 1], x[6:9, 3])))
y <- rowCollapse(x, 1:3)
print(y)
y_truth <- c(x[1, 1], x[2, 2], x[3, 3], x[4, 1], x[5, 2],
x[6, 3], x[7, 1], x[8, 2], x[9, 3])
stopifnot(identical(y, y_truth))
}
\seealso{
\emph{Matrix indexing} to index elements in matrices and arrays,
cf. \code{\link[base]{[}}().
}
\author{
Henrik Bengtsson
}
\keyword{utilities}
|