Last chance! 50% off unlimited learning
Sale ends in
A faster equivalent of tapply(FUN=sum)
.
tapplysum(x, flist, do.names = FALSE, na.rm = TRUE)
Vector of numeric or complex values.
A list of factor
s of the same length as x
.
Logical value indicating whether to attach names to the result.
Logical value indicating whether to remove NA
values
before computing the sums.
A numeric vector, matrix or array.
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
.
# NOT RUN {
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)
# }
Run the code above in your browser using DataLab