[R] sequentially summing up a elements of a vector

R. Michael Weylandt michael.weylandt at gmail.com
Sun Jan 22 01:02:13 CET 2012


Perhaps I misunderstand you, but ?cumsum.

x <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0)
y <- c(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4)
identical(cumsum(x), y)

Michael

On Sat, Jan 21, 2012 at 6:59 PM, Martin Batholdy
<batholdy at googlemail.com> wrote:
> Hi,
>
>
> I have a somewhat abstract and tricky problem (at least for me).
>
>
> Perhaps someone here can help me:
>
>
> I would like to convert:
>
> x <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0)
>
> into
>
> x <- c(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4)
>
>
> every 1 in x marks kind of a 'border' which is separated in different 'regions' in the new x.
>
>
>
> thanks for any suggestions!
>
> ______________________________________________
> 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