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.so...@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.so...@regione.marche.it >---Oo---------oO---------------- >________________________________ >Da: Bert Gunter [bgunter.4...@gmail.com] >Inviato: venerd� 28 agosto 2020 17.14 >A: Stefano Sofia >Cc: r-help mailing list >Oggetto: Re: [R] how to create a sequence to consecutive values > >Using ?rle > >> z <- rle(a) >> v <- z$values >> v[v==1] <- seq_along(v[v==1]) ## or use cumsum >< 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.so...@regione.marche.it<mailto:stefano.so...@regione.marche.it>> >wrote: >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.so...@regione.marche.it<mailto:stefano.so...@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. > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To >UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help<https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=52342f8a&h=d46bc785&f=y&p=y> >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html<https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=52342f8a&h=9b25bfd5&f=y&p=y> >and provide commented, minimal, self-contained, reproducible code. > >-- >Questo messaggio � stato analizzato con Libra ESVA ed � risultato non >infetto > >________________________________ > >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. > > > [[alternative HTML version deleted]] -- Sent from my phone. Please excuse my brevity. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.