R: Sum By Factor Level

tapplysum {spatstat.utils} | R Documentation

## 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 `factor`s 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.

### 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`.

### Value

A numeric vector, matrix or array.

### Author(s)

Adrian Baddeley Adrian.Baddeley@curtin.edu.au and Tilman Davies.

### See Also

`tapply`, `table`

### Examples

       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)

[Package spatstat.utils version 2.3-1 Index]