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 69
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/rowTabulates.R
\name{rowTabulates}
\alias{rowTabulates}
\alias{colTabulates}
\title{Tabulates the values in a matrix by row (column).}
\usage{
rowTabulates(x, rows = NULL, cols = NULL, values = NULL, ...,
useNames = TRUE)
colTabulates(x, rows = NULL, cols = NULL, values = NULL, ...,
useNames = TRUE)
}
\arguments{
\item{x}{An \code{\link[base]{integer}}, a \code{\link[base]{logical}}, or
a \code{\link[base]{raw}} NxK \code{\link[base]{matrix}}.}
\item{rows}{A \code{\link[base]{vector}} indicating subset of rows to
operate over. If \code{\link[base]{NULL}}, no subsetting is done.}
\item{cols}{A \code{\link[base]{vector}} indicating subset of columns to
operate over. If \code{\link[base]{NULL}}, no subsetting is done.}
\item{values}{An \code{\link[base]{vector}} of J values of count. If
\code{\link[base]{NULL}}, all (unique) values are counted.}
\item{...}{Not used.}
\item{useNames}{If \code{\link[base:logical]{TRUE}} (default), names
attributes of the result are set, otherwise not.}
}
\value{
Returns a NxJ (KxJ) \code{\link[base]{matrix}} where N (K) is the
number of row (column) \code{\link[base]{vector}}s tabulated and J is the
number of values counted.
}
\description{
Tabulates the values in a matrix by row (column).
}
\details{
An alternative to these functions, is to use \code{table(x, row(x))}
and \code{table(x, col(x))}, with the exception that the latter do not
support the \code{\link[base]{raw}} data type.
When there are no missing values in \code{x}, we have that
\code{all(rowTabulates(x) == t(table(x, row(x))))} and
\code{all(colTabulates(x) == t(table(x, col(x))))}.
When there are missing values, we have that
\code{all(rowTabulates(x) == t(table(x, row(x), useNA = "always")[, seq_len(nrow(x))]))} and
\code{all(colTabulates(x) == t(table(x, col(x), useNA = "always")[, seq_len(ncol(x))]))}.
}
\examples{
x <- matrix(1:5, nrow = 10, ncol = 5)
print(x)
print(rowTabulates(x))
print(colTabulates(x))
# Count only certain values
print(rowTabulates(x, values = 1:3))
y <- as.raw(x)
dim(y) <- dim(x)
print(y)
print(rowTabulates(y))
print(colTabulates(y))
}
\author{
Henrik Bengtsson
}
\keyword{utilities}
|