tapplysum | R Documentation |
A faster equivalent of tapply(FUN=sum)
.
tapplysum(x, flist, do.names = FALSE, na.rm = TRUE)
x |
Vector of numeric or complex values. |
flist |
A list of |
do.names |
Logical value indicating whether to attach names to the result. |
na.rm |
Logical value indicating whether to remove |
This function is designed to be a faster alternative to
the idiom y <- tapply(x, flist, sum); y[is.na(y)] <- 0
.
The result y
is a vector, matrix or array of
dimension equal to the number of factors in flist
. Each
position in y
represents one of the possible combinations
of the factor levels. The resulting value in this position
is the sum of all entries of x
where the factors in flist
take this particular combination of
values. The sum is zero if this combination does not occur.
Currently this is implemented for the cases where flist
has length 1, 2 or 3 (resulting in a vector, matrix or 3D array,
respectively).
For other cases we fall back on tapply
.
A numeric vector, matrix or array.
and Tilman Davies.
tapply
, table
x <- 1:12
a <- factor(rep(LETTERS[1:2], each=6))
b <- factor(rep(letters[1:4], times=3))
ff <- list(a, b)
tapply(x, ff, sum)
tapplysum(x, ff, do.names=TRUE)
tapplysum(x + 2i, ff, do.names=TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.