Open In App

Make Elements of a Vector Unique in R Programming – make.unique() Function

make.unique() function in R Language is used to return elements of a vector with unique names by appending sequence numbers to duplicates.

Syntax: make.unique(names, sep)
Parameters: 
names: Character vector with duplicate names 
sep: Separator to be used 
 



Example 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"))

Output:



[1] "a"   "a.1" "a.2"
[1] "a"   "b"   "c"   "a.1"
[1] "1"   "2"   "3"   "1.1" "2.1"

Example 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 = "$")

Output: 

[1] "a"   "a_1" "a_2"
[1] "a"   "a@1" "a@2"
[1] "a"   "a$1" "a$2"

 

Article Tags :