[R] RES: RES: Recode numbers

Filipe Leme Botelho filipe.botelho at vpar.com.br
Fri Jun 3 14:53:53 CEST 2011


Many thanks for this clever solution, it is much more elegant and efficient indeed. Cheers!

-----Mensagem original-----
De: Petr PIKAL [mailto:petr.pikal at precheza.cz] 
Enviada em: sexta-feira, 3 de junho de 2011 09:37
Para: Filipe Leme Botelho
Cc: Lisa; r-help at r-project.org
Assunto: Odp: [R] RES: Recode numbers

Hi

r-help-bounces at r-project.org napsal dne 01.06.2011 21:10:23:

> "Filipe Leme Botelho" <filipe.botelho at vpar.com.br> 
> Odeslal: r-help-bounces at r-project.org
> 
> RES: [R] Recode numbers
> 
> I think this is proper.
> 
> a <- c(1, 2, 3, 3, 4, 4, 5, 6, 1, 2, 2, 3, 1, 2, 1, 2, 3, 3, 4, 5, 1, 2,
> 3, 4)
> b <- c(1, 5, 8, 9, 14, 20, 3, 10, 12, 6, 16, 7, 11, 13, 17, 18, 2, 4,
> 15, 19)
> 
> ref <- 1
> for (i in 2:length(a)) {
>    if (a[i]!=a[i-1]) ref <- c(ref, ref[length(ref)]+1)
>    if (a[i]==a[i-1]) ref <- c(ref, ref[length(ref)])
>    }
> 
> a2 <-b[ref]
> a1 <- c(1, 5, 8, 8, 9, 9, 14, 20, 3, 10, 10, 12, 6, 16, 7, 11, 13, 13,
> 17, 18, 2, 4, 15, 19)

a bit shorter is

a2<-rep(b, rle(a)$lengths)

Regards
Petr

> 
> a1==a2
> 
> 
> -----Mensagem original-----
> De: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> Em nome de Duncan Murdoch
> Enviada em: quarta-feira, 1 de junho de 2011 15:51
> Para: Lisa
> Cc: r-help at r-project.org
> Assunto: Re: [R] Recode numbers
> 
> (The attributions are a little messed up here:)
> 
> >> I have two sets of numbers that look like
> >>> a<- c(1, 2, 3, 3, 4, 4, 5, 6, 1, 2, 2, 3, 1, 2, 1, 2, 3, 3, 4, 5, 1,
> 2, 3,
> >>> 4)
> >>>
> >>> b<- c(1, 5, 8, 9, 14, 20, 3, 10, 12, 6, 16, 7, 11, 13, 17, 18, 2, 4,
> 15,
> >>> 19)
> >>>
> >>> I just want to use "b" to encode "a" so that "a" looks like
> >>>
> >>> a1<- c(1, 5, 8, 8, 9, 9, 14, 20, 3, 10, 10, 12, 6, 16, 7, 11, 13,
> 13, 17,
> >>> 18, 2, 4, 15, 19)
> >>>
> >>> Does anyone have a suggestion how to deal with this? Thank you in
> advance.
> >>>
> >>
> >> is a1 = b[a] what you are looking for?
> >>
> >> HTH
> >>
> >> Pete
> >
> > Thank you for your help, Pete. I tried b[a], but it is not a1.
> 
> 
> I think you'll need to explain the rule used for a1[9], a1[10], etc.
> 
> Duncan Murdoch
> 
> ______________________________________________
> 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.
> "This message and its attachments may contain confidential and/or 
> privileged information. If you are not the addressee, please, advise the 

> sender immediately by replying to the e-mail and delete this message."
> 
> "Este mensaje y sus anexos pueden contener información confidencial o 
> privilegiada. Si ha recibido este e-mail por error por favor bórrelo y 
> envíe un mensaje al remitente."
> 
> "Esta mensagem e seus anexos podem conter informação confidencial ou 
> privilegiada. Caso não seja o destinatário, solicitamos a imediata 
> notificação ao remetente e exclusão da 
> mensagem."______________________________________________
> 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.


"This message and its attachments may contain confidential and/or privileged information. If you are not the addressee, please, advise the sender immediately by replying to the e-mail and delete this message."

"Este mensaje y sus anexos pueden contener información confidencial o privilegiada. Si ha recibido este e-mail por error por favor bórrelo y envíe un mensaje al remitente."

"Esta mensagem e seus anexos podem conter informação confidencial ou privilegiada. Caso não seja o destinatário, solicitamos a imediata notificação ao remetente e exclusão da mensagem."



More information about the R-help mailing list