Assign numbers in R

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Assign numbers in R

tbal
This post has NOT been accepted by the mailing list yet.
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!