[R] partial cumsum
Karl Ove Hufthammer
karl at huftis.org
Wed Nov 11 18:11:51 CET 2009
On Wed, 11 Nov 2009 08:53:50 -0800 William Dunlap <wdunlap at tibco.com>
wrote:
> > x <- c(1, 2, 3, NA, 5, 6, 7, 8, 9, 10)
> >
> > should become this:
> >
> > 1 3 6 NA 5 11 18 26 35 45
>
> Perhaps
> > ave(x, rev(cumsum(rev(is.na(x)))), FUN=cumsum)
> [1] 1 3 6 NA 5 11 18 26 35 45
Clever way of putting the NA values at the end of each group. (I had to
study it quite a bit to understand what was going on, and why! :-) )
I wish cumsum took a 'na.rm' argument, though. It would make stuff like
this much easier.
--
Karl Ove Hufthammer
More information about the R-help
mailing list