Learn R Programming

spatstat.utils (version 3.2-5)

tapplysum: Sum By Factor Level

Description

A faster equivalent of tapply(FUN=sum).

Usage

tapplysum(x, flist, do.names = FALSE, na.rm = TRUE)

Arguments

Value

A numeric vector, matrix or array.

Details

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.

See Also

Examples

Run this code
   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