Make a column with duplicated values unique in a dataframe

后端 未结 1 968
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 18:56

I have a dataframe where a column has duplicate values like

employee <- data.frame(name = c(\'John\', \'Joe\', \'Mat\', \'John\', \'Joe\'), 
                     


        
相关标签:
1条回答
  • 2020-12-06 19:05

    We can use make.names with unique=TRUE. By default, a . will be appended before the suffix numbers, and that can be replaced by _ using sub

     employee$name <- sub('[.]', '_', make.names(employee$name, unique=TRUE))
    

    Or a better option suggested by @DavidArenburg. If the name column is factor class, convert the input column to character class (as.character) before applying the make.unique

     make.unique(as.character(employee$name), sep = "_")
     #[1] "John"   "Joe"    "Mat"    "John_1" "Joe_1" 
    
    0 讨论(0)
提交回复
热议问题