Compute column sums across rows of a numeric matrix-like object for each level of a grouping variable. rowsum is generic, with a method for data frames and a default method for vectors and matrices.
rowsum(x, group, reorder = TRUE, …)<p></p><p># S3 method for data.frame
rowsum(x, group, reorder = TRUE, na.rm = FALSE, …)</p><p># S3 method for default
rowsum(x, group, reorder = TRUE, na.rm = FALSE, …)</p>
| Parameter | Description |
|---|---|
x |
a matrix, data frame or vector of numeric data. Missing values are allowed. A numeric vector will be treated as a column vector. |
group |
a vector or factor giving the grouping, with one element per row of x. Missing values will be treated as another group and a warning will be given. |
reorder |
if TRUE, then the result will be in order of sort(unique(group)), if FALSE, it will be in the order that groups were encountered. |
na.rm |
logical (TRUE or FALSE). Should NA (including NaN) values be discarded? |
… |
other arguments to be passed to or from methods |
# NOT RUN {
require(stats)
x <- matrix(runif(100), ncol = 5)
group <- sample(1:8, 20, TRUE)
(xsum <- rowsum(x, group))
## Slower versions
tapply(x, list(group[row(x)], col(x)), sum)
t(sapply(split(as.data.frame(x), group), colSums))
aggregate(x, list(group), sum)[-1]
# }