R语言 使向量中的元素唯一 – make.unique()函数
R语言中的 make.unique() 函数用于通过给重复的元素加上序列号来返回具有唯一名称的向量元素。
语法: make.unique(names, sep)
参数:
names: 具有重复名称的字符向量
sep: 要使用的分隔符
例1 :
# R program to make unique vectors
# Calling make.unique() Function
make.unique(c("a", "a", "a"))
make.unique(c("a", "b", "c", "a"))
make.unique(c("1", "2", "3", "1", "2"))
输出
[1] "a" "a.1" "a.2"
[1] "a" "b" "c" "a.1"
[1] "1" "2" "3" "1.1" "2.1"
例2 :
# R program to make unique vectors
# Calling make.unique() Function
# with different separator values
make.unique(c("a", "a", "a"), sep = "_")
make.unique(c("a", "a", "a"), sep = "@")
make.unique(c("a", "a", "a"), sep = "$")
输出
[1] "a" "a_1" "a_2"
[1] "a" "a@1" "a@2"
[1] "a" "a1" "a2"