简体   繁体   中英

create new column of unique identifier based off column with duplicates

I have a column that has id_numbers, however some aren't unique:

alpha_id
       1
       2
       2
       3
       4

I want a new column that keeps the id if it is unique, but labels it 2a and 2b if it is not unique

alpha_id    unique_id
       1            1
       2           2a
       2           2b
       3            3
       4            4

not sure where to start

a = data.frame(alpha = c(1, 2, 2, 3, 4))    
a$unique = paste0(a$alpha, ave(a$alpha, a$alpha,
                  FUN = function(x) if(length(x) >= 2){letters[seq_along(x)]}else{""}))
a
#  alpha unique
#1     1      1
#2     2     2a
#3     2     2b
#4     3      3
#5     4      4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM