[R] partial cumsum

William Dunlap wdunlap at tibco.com
Wed Nov 11 17:53:50 CET 2009



Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of smu
> Sent: Wednesday, November 11, 2009 7:58 AM
> To: r-help at r-project.org
> Subject: [R] partial cumsum
> 
> Hello,
> 
> I am searching for a function to calculate "partial" cumsums.
> 
> For example it should calculate the cumulative sums until a 
> NA appears,
> and restart the cumsum calculation after the NA.
> 
> this:
> 
> 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

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
 
> any ideas?
> 
> thank you and best regards,
> 
>     stefan
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 




More information about the R-help mailing list