On Mon, Jul 16, 2012 at 12:17 PM, mdvaan <mathijsdev...@gmail.com> wrote: > Hi, > > I have the following sequence: > in <- c(0, 0, 0, 2, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 0, 2, 0, 0, 2) > > >From this sequence I would like to get to the following sequence: > out <- c(0, 0, 0, 3, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, > 0, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 0, 2, 0, 2, 0, 0, 2) > > Basically, what I would like to do for each number greater than 0, is to > add all adjacent numbers and the adjacent numbers of those numbers, etc. > until one of those numbers is equal to 0. >
Here cumsum(input == 0) * (input !=0) replaces each run of non-zeros with a distinct group number and then ave is used to sum over the distinct groups: > ave(input, cumsum(input == 0) * (input != 0), FUN = sum) [1] 0 0 0 3 3 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 11 11 11 11 11 11 11 11 11 11 11 0 2 0 2 0 0 2 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-help@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.