tapplysum.fast: Simplified fast group sums

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/eco.R

Description

A hack to speed up tapply(x, group, sum) for the special case where x is sorted by group.

Usage

1
tapplysum.fast(x, groups)

Arguments

x

A numeric vector.

groups

A grouping factor.

Details

Works by computing the cumulative sum of x and taking the difference at the indices where the groups change. Standard tapply can be slow when there are a large number of groups, due to the overhead of factor manipulation.

Value

Vector containing the group sums of x.

Author(s)

C. H. Jackson <chris.jackson@mrc-bsu.cam.ac.uk>

See Also

tapply

Examples

1
2
3
4
x <- factor(rep(1:1000, each=100))
y <- rnorm(1000*100)
system.time(tapply(y, x, sum))
system.time(tapplysum.fast(y, x))

ecoreg documentation built on March 26, 2020, 7:39 p.m.