To remove duplicates in a vector in R, you can use the unique() function.
In this example,
vec
with duplicate values.unique()
function to vec
to remove duplicates.unique()
function returns a vector with unique elements.vec <- c(1, 2, 2, 3, 4, 4, 5)
unique_vec <- unique(vec)
print(unique_vec)
[1] 1 2 3 4 5
In this example,
names_vec
with duplicate names.unique()
function to names_vec
to remove duplicate names.unique()
function returns a vector with unique names.names_vec <- c("Alice", "Bob", "Alice", "Charlie", "Bob")
unique_names_vec <- unique(names_vec)
print(unique_names_vec)
[1] "Alice" "Bob" "Charlie"
In this tutorial, we learned How to Remove Duplicates in a Vector in R language with well detailed examples.