spatstat.utils (version 1.17-0)

tapplysum: Sum By Factor Level

Description

A faster equivalent of tapply(FUN=sum).

Usage

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

Arguments

x

Vector of numeric or complex values.

flist

A list of factors of the same length as x.

do.names

Logical value indicating whether to attach names to the result.

na.rm

Logical value indicating whether to remove NA values before computing the sums.

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

tapply, table

Examples

Run this code
# 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 DataCamp Workspace