Dear all, 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. Lisa -- View this message in context: http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566395.html Sent from the R help mailing list archive at Nabble.com.
Lisa wrote:> > Dear all, > > 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. > > Lisa >is a1 = b[a] what you are looking for? HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566505.html Sent from the R help mailing list archive at Nabble.com.
Thank you for your help, Pete. I tried b[a], but it is not a1. -- View this message in context: http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566534.html Sent from the R help mailing list archive at Nabble.com.
Thank you so much, Filipe. Your R script is what I am looking for. -- View this message in context: http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566818.html Sent from the R help mailing list archive at Nabble.com.
Thank you very much, Bill. Your script works very well. -- View this message in context: http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566847.html Sent from the R help mailing list archive at Nabble.com.
Hi: Here's another option: rep(b, rle(a)$lengths)> identical(a1, rep(b, rle(a)$lengths))[1] TRUE rle(a)$lengths computes a table of the number of consecutive repeats of a number (or run lengths). It will have the same length as b in this case. Using rep() with the table of lengths as repetitions gets the desired result:> rep(b, rle(a)$lengths)[1] 1 5 8 8 9 9 14 20 3 10 10 12 6 16 7 11 13 13 17 18 2 4 15 19 HTH, Dennis On Wed, Jun 1, 2011 at 10:19 AM, Lisa <lisajca at gmail.com> wrote:> Dear all, > > 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. > > Lisa > > > -- > View this message in context: http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566395.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >