[R-SIG-Finance] Sequence ID
Jorge Nieves
jorge.nieves at moorecap.com
Wed Sep 10 17:38:51 CEST 2008
Thanks,
I received two suggestions that work..
One by Martin Becker
test = c(0,0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,0)
myrle<-rle(test)
myrle$values[myrle$values==1]<-1000*seq_len(sum(myrle$values==1))
inverse.rle(myrle)
And the other by Gabor (see bellow)
Thanks a lot !!!!
Jorge
-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Sent: Wednesday, September 10, 2008 11:36 AM
To: Jorge Nieves
Cc: r-sig-finance at stat.math.ethz.ch
Subject: Re: [R-SIG-Finance] Sequence ID
Try this:
1000 * cumsum(pmax(0, diff(c(0, test)))) * test
On Wed, Sep 10, 2008 at 10:49 AM, Jorge Nieves
<jorge.nieves at moorecap.com> wrote:
>
> Hi,
>
> First I would like to apologize if this is not the right forum for the
> following question.
>
> I have the following sequence of ZEROS and ONES. I would like to ID
> each "package" of ONES with a unique identifier.
>
> test = c(0,0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,0)
>
> An example could be the following vector
>
> id
> =c(0,0,0,0,1000,1000,1000,1000,0,0,0,2000,2000,2000,2000,2000,0,0,0,30
> 00
> ,3000,3000,0)
>
> Where all the consecutive ONES have the same ID. I can create the ID
> using a loop, but I was wondering if anyone could recommend another
> method. I suspect that something in the area of factors might help,
> but I do not know what will be the best approach. Any recommendations
> will be highly appreciated.
>
>
>
> Jorge Nieves
> Phone 212.782.7083
> Fax 212.642.7644
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
More information about the R-SIG-Finance
mailing list