Q:

Modify same values of a variable in R

My data frame is like this:
Name   No
X      1
X      3
YYY    1
YYY    6
X      8
X      2
X      1

I want to change the name of the last 3 X to something not X (X1 for example). Basically same values but not continuous must be modified. There are many cases like this in my Name variable. Is there any way to do it in R? It is quite like not-sorted in SAS.
Thank you very much

A:

rle suggests itself here for "same values but not continuous must be modified"
r <- rle(as.character(df$Name))
#Run Length Encoding
#  lengths: int [1:3] 2 2 3
#  values : chr [1:3] "X" "YYY" "X"

r$values <- make.unique(r$values,sep="")
inverse.rle(r)
#[1] "X"   "X"   "YYY" "YYY" "X1"  "X1"  "X1"

df$Name <- inverse.rle(r)
df

#  Name No
#1    X  1
#2    X  3
#3  YYY  1
#4  YYY  6
#5   X1  8
#6   X1  2
#7   X1  1

