Reputation: 165
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
Upvotes: 1
Views: 51
Reputation: 32538
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
Upvotes: 3