Hi, I have the following numbers: d <- c(8,7,5,5,3,3,2,1,1,1) I want to convert these into the following numbers: r: 1,2,3,3,4,4,5,6,6,6 So if two numbers are different increment it if they are same then assign the same number: r <- NULL for (i in 1:length(d)) { if (d[i] != d[i+1]) { r[i] =i+1; } else { r[i] = i; } } But this is not correct. How can I solve this problem? or how can I solve it in a different way? Thanks a lot! [[alternative HTML version deleted]]
Hello, For your example, the following will work: R> d <- c(8,7,5,5,3,3,2,1,1,1) R> idx <- 1:length(unique(d)) R> rep(idx, rle(d)$length) [1] 1 2 3 3 4 4 5 6 6 6 HTH, Pascal On Wed, Mar 12, 2014 at 6:13 PM, T Bal <studenttbal at gmail.com> wrote:> Hi, > I have the following numbers: > > d <- c(8,7,5,5,3,3,2,1,1,1) > > I want to convert these into the following numbers: > > r: > 1,2,3,3,4,4,5,6,6,6 > > So if two numbers are different increment it if they are same then assign > the same number: > > r <- NULL > > for (i in 1:length(d)) { > > if (d[i] != d[i+1]) { > r[i] =i+1; > } > else { > r[i] = i; > } > } > > But this is not correct. How can I solve this problem? or how can I solve > it in a different way? Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Pascal Oettli Project Scientist JAMSTEC Yokohama, Japan
Hello, Try the following. r <- cumsum(c(TRUE, diff(d) != 0)) Hope this helps, Rui Barradas Em 12-03-2014 09:13, T Bal escreveu:> Hi, > I have the following numbers: > > d <- c(8,7,5,5,3,3,2,1,1,1) > > I want to convert these into the following numbers: > > r: > 1,2,3,3,4,4,5,6,6,6 > > So if two numbers are different increment it if they are same then assign > the same number: > > r <- NULL > > for (i in 1:length(d)) { > > if (d[i] != d[i+1]) { > r[i] =i+1; > } > else { > r[i] = i; > } > } > > But this is not correct. How can I solve this problem? or how can I solve > it in a different way? Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Hi, is this homework? Try d <- c(8,7,5,5,3,3,2,1,1,1) r <- rep(1,length(d)) for (i in 2:length(d)) { if (d[i] != d[i-1]) { r[i]=r[i-1]+1; } else { r[i] = r[i-1]; } } Although I am sure there are better solutions! HTH daniel ________________________________________ Felad?: r-help-bounces at r-project.org [r-help-bounces at r-project.org] ; meghatalmazó: T Bal [studenttbal at gmail.com] K?ldve: 2014. m?rcius 12. 10:13 To: r-help at r-project.org T?rgy: [R] Assign numbers in R Hi, I have the following numbers: d <- c(8,7,5,5,3,3,2,1,1,1) I want to convert these into the following numbers: r: 1,2,3,3,4,4,5,6,6,6 So if two numbers are different increment it if they are same then assign the same number: r <- NULL for (i in 1:length(d)) { if (d[i] != d[i+1]) { r[i] =i+1; } else { r[i] = i; } } But this is not correct. How can I solve this problem? or how can I solve it in a different way? Thanks a lot! [[alternative HTML version deleted]] ______________________________________________ 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.
Hi, Try: ?cumsum(c(TRUE,d[-1]!=d[-length(d)])) A.K. On Wednesday, March 12, 2014 5:28 AM, T Bal <studenttbal at gmail.com> wrote: Hi, I have the following numbers: d <- c(8,7,5,5,3,3,2,1,1,1) I want to convert these into the following numbers: r: 1,2,3,3,4,4,5,6,6,6 So if two numbers are different increment it if they are same then assign the same number: r <- NULL for (i in 1:length(d)) { if (d[i] != d[i+1]) { ? r[i] =i+1; } else { ? r[i] = i; } } But this is not correct. How can I solve this problem? or how can I solve it in a different way? Thanks a lot! ??? [[alternative HTML version deleted]] ______________________________________________ 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.
Here are a couple more options if you want some variety:> d <- c(8,7,5,5,3,3,2,1,1,1) > as.numeric( factor(d, levels=unique(d)) )[1] 1 2 3 3 4 4 5 6 6 6> cumsum( !duplicated(d) )[1] 1 2 3 3 4 4 5 6 6 6 What would you want the output to be if your d vector had another 8 after the last 1? The different solutions will give different output. On Wed, Mar 12, 2014 at 3:13 AM, T Bal <studenttbal at gmail.com> wrote:> Hi, > I have the following numbers: > > d <- c(8,7,5,5,3,3,2,1,1,1) > > I want to convert these into the following numbers: > > r: > 1,2,3,3,4,4,5,6,6,6 > > So if two numbers are different increment it if they are same then assign > the same number: > > r <- NULL > > for (i in 1:length(d)) { > > if (d[i] != d[i+1]) { > r[i] =i+1; > } > else { > r[i] = i; > } > } > > But this is not correct. How can I solve this problem? or how can I solve > it in a different way? Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com