x <- c("apple", "banana", "apple", "orange", "banana")Removing Duplicate Vector Elements in R
r
A note on using the
make.unique() function to make duplicate vector elements unique.
Note
Original Japanese version: Rでベクトルの重複を取り除く方法
Using the make.unique() Function
This is a note on how to handle duplicate elements in a vector in R and turn them into unique elements. With the make.unique() function, duplicate elements can be automatically converted into unique values by adding suffixes. The usage is as follows.
make.unique(names, sep = ".")-
names: a character vector you want to make unique -
sep: the separator used before the suffix added to duplicate elements; the default is a dot,.
Example
Suppose we have the following vector.
Applying make.unique() to this vector gives the following result.
unique_x <- make.unique(x)
print(unique_x)[1] "apple" "banana" "apple.1" "orange" "banana.1"
Because apple and banana are duplicated, .1 is added after the repeated words.
Closing Note
I cannot remember exactly where I used this function, but I remembered using it before and left this note. The opportunities to use it may be limited, but it can be useful to know.