[R] blockwise sums

Thomas Lumley tlumley at u.washington.edu
Tue Aug 31 15:40:32 CEST 2004


On Tue, 31 Aug 2004, Liaw, Andy wrote:

> If you insist, here's one way:
>
> my.blockwisesum <- function(x, n, ...) {
>     tapply(x, seq(1, length(x), by=n), sum, ...)
> }

If x is large, rowsum() should be faster than tapply().

	-thomas


>
> Andy
>
> > From: Lutz Prechelt
> >
> > I am looking for a function like
> >   my.blockwisesum(vector, n)
> > that computes sums of disjoint subsequences of length n from vector
> > and can work with vector lengths that are not a multiple of n.
> >
> > It should give me for instance
> >   my.blockwisesum(1:10, 3) == c(6, 15, 24, 10)
> >
> > Is there a builtin function that can do this?
> > One could do it by coercing the vector into a matrix of width n,
> > and then use apply,
> > but that is cumbersome if the length is not divisible by n,
> > is it not?
> > Any other ideas?
> >
> >   Lutz
> >
> > Prof. Dr. Lutz Prechelt;  prechelt at inf.fu-berlin.de
> > Institut fuer Informatik; Freie Universitaet Berlin
> > Takustr. 9; 14195 Berlin; Germany
> > +49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle




More information about the R-help mailing list