I would like to identify duplicates and then add the sequential number before the first character. In the script below i identified the duplicates
I have a dataset that looks like this
col|
X123
X123
X456
X789
X890
X142
X142
X142
df$col<- ifelse(duplicated(df[,c("col")])|duplicated(df[,c("col")],fromLast = TRUE),
make.unique(df$col),df$col)
What my script ends up doing is this
col|
X123
X123.1
X456
X789
X890
X142
X142.1
X142.2
What I would like for it to do is
col|
1X123
2X123
X456
X789
X890
1X142
2X142
3X142
>Solution :
Define a function which prepends sequence numbers and then use it with ave.
add_seq <- function(x) if (length(x) == 1) x else paste0(seq_along(x), x)
transform(DF, col = ave(col, col, FUN = add_seq))
giving:
col
1 1X123
2 2X123
3 X456
4 X789
5 X890
6 1X142
7 2X142
8 3X142
Note
Lines <- "col
X123
X123
X456
X789
X890
X142
X142
X142"
DF <- read.table(text = Lines, header = TRUE, strip.white = TRUE)