Fix duplicated values in a character vector, useful in column names and some ID structures that requires unique identifiers. If any duplicated string is found in the vector, a numeric index will be added after the these strings.

strUniquefy(x, sep_b = "_", sep_a = "", mark_first = TRUE)

Arguments

x

character vector

sep_b

string, separator before the number index

sep_a

string, separator after the number index

mark_first

bool, if duplicated values are found, do you want to add the numeric index starting from the first copy? FALSE means starting from the second copy.

Value

returns a character vector

Details

The input can also be a numeric vector, but the return will always be character.

Examples

strUniquefy(c(1,1,1,2,3))
#> [1] "1_1" "1_2" "1_3" "2"   "3"  
strUniquefy(c(1,1,1,2,3), mark_first = FALSE)
#> [1] "1"   "1_1" "1_2" "2"   "3"  
strUniquefy(c(1,1,1,2,3), sep_b = "(", sep_a = ")")
#> [1] "1(1)" "1(2)" "1(3)" "2"    "3"   
strUniquefy(c("a","b","c","a","d","b"))
#> [1] "a_1" "b_1" "c"   "a_2" "d"   "b_2"