I have a dataframe where a column has duplicate values like
employee <- data.frame(name = c(\'John\', \'Joe\', \'Mat\', \'John\', \'Joe\'),
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"