當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 make.unique()用法及代碼示例


R 語言中的 make.unique() 函數用於通過將序列號附加到重複項來返回具有唯一名稱的向量元素。

用法: make.unique(names, sep)
參數: 
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"))

輸出:



[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"

相關用法


注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Make Elements of a Vector Unique in R Programming – make.unique() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。