在 R 编程中使向量的元素唯一 – make.unique()函数
R 语言中的make.unique()函数用于通过将序列号附加到重复项来返回具有唯一名称的向量的元素。
Syntax: make.unique(names, sep)
Parameters:
names: Character vector with duplicate names
sep: Separator to be used
示例 1:
Python3
# 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"))
Python3
# 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" "b" "c" "a.1"
[1] "1" "2" "3" "1.1" "2.1"
示例 2:
Python3
# 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" "a$1" "a$2"