Skip to content
Related Articles

Related Articles

Make Elements of a Vector Unique in R Programming – make.unique() Function
  • Last Updated : 16 Jun, 2020

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: Separater 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 separater 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"
My Personal Notes arrow_drop_up
Recommended Articles
Page :