Re: [R] unique identifier for number sequence

2015-11-23 Thread Eik Vettorazzi
cumsum(c(x[1],pmax(0,diff(x*x Am 23.11.2015 um 15:59 schrieb PIKAL Petr: > Dear all > > I have a vector ones and zeroes like that > x<-c(rep(0,5), rep(1,5), rep(0,10), rep(1,8)) > > and I need to get result like that > x.i<-c(rep(0,5), rep(1,5), rep(0,10), rep(2,8)) > > It means I need an u

Re: [R] unique identifier for number sequence

2015-11-23 Thread William Dunlap
> f <- function(x)cumsum(c(x[1]==1, x[-length(x)]==0 & x[-1]==1)) * (x==1) > f(x) [1] 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 > f(rev(x)) [1] 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Nov 23, 2015 at 6:59 AM, PIK

Re: [R] unique identifier for number sequence

2015-11-23 Thread jim holtman
[mailto:jholt...@gmail.com] > *Sent:* Monday, November 23, 2015 4:14 PM > *To:* PIKAL Petr > *Cc:* r-help@r-project.org > *Subject:* Re: [R] unique identifier for number sequence > > > > Here is one way of doing it: > > > > > x<-c(rep(0,5), rep(1,5), rep(0,10)

Re: [R] unique identifier for number sequence

2015-11-23 Thread PIKAL Petr
Hi Cool, thanks. I knew I am missing some obvious way. Cheers Petr From: jim holtman [mailto:jholt...@gmail.com] Sent: Monday, November 23, 2015 4:14 PM To: PIKAL Petr Cc: r-help@r-project.org Subject: Re: [R] unique identifier for number sequence Here is one way of doing it: > x<-c(r

Re: [R] unique identifier for number sequence

2015-11-23 Thread jim holtman
Here is one way of doing it: > x<-c(rep(0,5), rep(1,5), rep(0,10), rep(1,8)) > x [1] 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 > > # mark changes from 0->1 and create increments > indx <- cumsum(c(FALSE, diff(x) == 1)) > > # keep just matches with '1' > x.i <- ifelse(x == 1, indx, 0