
rowsum
is generic, with a
method for data frames and a default method for vectors and matrices.
rowsum(x, group, reorder = TRUE, ...)
"rowsum"(x, group, reorder = TRUE, na.rm = FALSE, ...)
"rowsum"(x, group, reorder = TRUE, na.rm = FALSE, ...)
x
. Missing values will be treated as another
group and a warning will be given.TRUE
, then the result will be in order of
sort(unique(group))
, if FALSE
, it will be in the order
that groups were encountered.TRUE
or FALSE
). Should NA
(including NaN
) values be discarded?group
.
tapply
as in
the example below. Reordering should not add noticeably to the time
except when there are very many distinct values of group
and
x
has few columns.The original function was written by Terry Therneau, but this is a new implementation using hashing that is much faster for large matrices.
To sum over all the rows of a matrix (ie, a single group
) use
colSums
, which should be even faster.
For integer arguments, over/underflow in forming the sum results in
NA
.
tapply
, aggregate
, rowSums
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]
Run the code above in your browser using DataLab