[R] Finding and manipulation clusters of numbers in a sequence of numbers

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jul 16 21:47:05 CEST 2012


On Mon, Jul 16, 2012 at 12:17 PM, mdvaan <mathijsdevaan at 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



More information about the R-help mailing list