Make Elements of a Character Vector Unique (Numbering From 1)
make_unique_all.RdA variant of base::make.unique() that appends numeric suffixes
to elements of a character vector. Unlike make.unique(), which
leaves the first occurrence unchanged, this function can number
all members of a duplicated group starting from 1. Optionally,
it can number singletons as well.
Value
A character vector of the same length as x,
with duplicates disambiguated by appending a numeric suffix.
Examples
# Mimics make.unique() but numbers first duplicates as well
make_unique_all(c("A", "A", "B"))
#> [1] "A.1" "A.2" "B.1"
# [1] "A.1" "A.2" "B"
# Multiple duplicate groups
make_unique_all(c("A", "A", "B", "B", "C"))
#> [1] "A.1" "A.2" "B.1" "B.2" "C.1"
# [1] "A.1" "A.2" "B.1" "B.2" "C"
# Force suffixes on all elements
make_unique_all(c("A", "A", "B"), always = TRUE)
#> [1] "A.1" "A.2" "B.1"
# [1] "A.1" "A.2" "B.1"
# Custom separator
make_unique_all(c("X", "X", "Y"), sep = "_")
#> [1] "X_1" "X_2" "Y_1"
# [1] "X_1" "X_2" "Y"