Skip to content

how to create a sequence to consecutive values

6 messages · Stefano Sofia, Jeff Newmiller, Bert Gunter

#
Dear R-list users,
this is a simple question, I have not been able to find an efficient solution.
Given a vector with only 0 or 1 values, I need to give a sequence to the consecutive values of 1:

a <- c(0,0,0,1,1,1,1,0,0,0,0,1,1,0,1,1,1,0,0)

I should get as result

(0,0,0,1,1,1,1,0,0,0,0,2,2,0,3,3,3,0,0)

I tried with ave, but no way to get it for me.

Thank you for your help
Stefano

         (oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Civil Protection - Marche Region
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona
Uff: 071 806 7743
E-mail: stefano.sofia at regione.marche.it
---Oo---------oO----------------

________________________________

AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu? contenere informazioni confidenziali, pertanto ? destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si ? il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si ? ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell?art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit? ed urgenza, la risposta al presente messaggio di posta elettronica pu? essere visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.

--
Questo messaggio  stato analizzato da Libra ESVA ed  risultato non infetto.
This message was scanned by Libra ESVA and is believed to be clean.
#
Using ?rle
< rep(v,z$lengths)
 [1] 0 0 0 1 1 1 1 0 0 0 0 2 2 0 3 3 3 0 0

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Aug 28, 2020 at 7:52 AM Stefano Sofia <
stefano.sofia at regione.marche.it> wrote:

            

  
  
#
Thank you!
Stefano

         (oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Civil Protection - Marche Region
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona
Uff: 071 806 7743
E-mail: stefano.sofia at regione.marche.it
---Oo---------oO----------------
#
cumsum is a bit faster...

a <- c( 0, 0, 0, 1, 1, 1, 1, 0, 0, 0
      , 0, 1, 1, 0, 1, 1, 1, 0
      )

f1 <- function(a) {
  z <- rle(a)
  v <- z$values
  v[v==1] <- seq_along(v[v==1]) ## or use cumsum
  rep(v,z$lengths)
}

f2 <- function(a) {
  v <- cumsum( c( a[1], 1==diff(a) ) )
  v[ 0==a ] <- 0
  v
}

f2(a)

library(microbenchmark)

a2 <- rep( c( 0,0, 1, 1, 1 )
         , 300 )

microbenchmark( res1 <- f1(a2)
              , res2 <- f2(a2)
              )
stopifnot( res1 == res2 )
On August 28, 2020 8:19:41 AM PDT, Stefano Sofia <stefano.sofia at regione.marche.it> wrote:

  
    
#
Actually, I prefer Jeff's use of diff() . Hadn't thought of that.

However, note that, unsurprisingly,  NA's mess up both: The rle() method
fails with an error and the diff() method gives the wrong answer.

Cheers,
Bert


On Fri, Aug 28, 2020 at 8:48 AM Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:

  
  
2 days later
#
Thank you both of you. I am studying these solutions.

Stefano

         (oo)
--oOO--( )--OOo----------------
Stefano Sofia PhD
Civil Protection - Marche Region
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona
Uff: 071 806 7743
E-mail: stefano.sofia at regione.marche.it
---Oo---------oO----------------