[R] Assign numbers in R

2014-03-12 Thread T Bal
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]

Re: [R] Assign numbers in R

2014-03-12 Thread Pascal Oettli
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 studentt...@gmail.com wrote: Hi, I have the following numbers: d -

Re: [R] Assign numbers in R

2014-03-12 Thread Rui Barradas
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

Re: [R] Assign numbers in R

2014-03-12 Thread Kehl Dániel
-help-boun...@r-project.org [r-help-boun...@r-project.org] ; meghatalmaz#243;: T Bal [studentt...@gmail.com] Küldve: 2014. március 12. 10:13 To: r-help@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

Re: [R] Assign numbers in R

2014-03-12 Thread arun
Hi, Try:  cumsum(c(TRUE,d[-1]!=d[-length(d)])) A.K. On Wednesday, March 12, 2014 5:28 AM, T Bal studentt...@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

Re: [R] Assign numbers in R

2014-03-12 Thread T Bal
-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

Re: [R] Assign numbers in R

2014-03-12 Thread Greg Snow
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

Re: [R] Assign numbers in R

2014-03-12 Thread Arunkumar Srinivasan
Here's another one: match(d, unique(d)). Arun From: Greg Snow 538...@gmail.com Reply: Greg Snow 538...@gmail.com Date: March 12, 2014 at 8:41:31 PM To: T Bal studentt...@gmail.com Cc: r-help r-help@r-project.org Subject:  Re: [R] Assign numbers in R Here are a couple more options