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. Michael Weylandt
2012-Jan-22 00:02 UTC
[R] sequentially summing up a elements of a vector
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.
Try cumsum(x) HTH, Jorge.- On Sat, Jan 21, 2012 at 6:59 PM, Martin Batholdy <> 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@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. >[[alternative HTML version deleted]]