Do you want to increment every duplicate? Such that c("Ashish","Ricky","Sagar","Ashish","Sagar","Ashish","Ricky")
gets changed to c("Ashish1","Ricky1","Sagar1","Ashish2","Sagar2","Ashish3","Ricky2")
?
I've opted to increase all customer names (is there a reason only the very first should not be added an incremented value?) simply because it's easier. But I will show that once we can do that, it is easy to only add to every subsequent:
Introducing the package dplyr
:
customer %>% group_by(name) %>%
mutate(i = 1:n()) %>% ungroup %>%
mutate(name = paste0(name, i)
If you have any ordering to determine which name is the "first", e.g. customerId
, add arrange(customerId) %>%
after the first group_by
.
Now that we have added an increasing number, let's modify it, so the very first is not modified:
customer %>% group_by(name) %>%
mutate(i = 1:n()) %>% ungroup %>%
mutate(name = ifelse(i > 1, paste0(name, i), name))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…