0

I have a vector of name combinations like so:

vec <- c("US;DE;US", "AU;AU;JP", "IN;SA;CN;RU", "PK;IQ;IQ")

I want to keep only the unique names in each vector, i.e., the final vector should look as follows:

vec <- c("US;DE", "AU;JP", "IN;SA;CN;RU", "PK;IQ")
Muhammad Kamil
  • 635
  • 2
  • 15

1 Answers1

1

With base R, we can use strsplit + unique + paste0 to make it

> sapply(strsplit(vec, ";"), function(x) paste0(unique(x), collapse = ";"))
[1] "US;DE"       "AU;JP"       "IN;SA;CN;RU" "PK;IQ"
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81