Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add sequential values to identified duplicates before first character?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading