0

Suppose the next vector:

just_a_random_vector <- c("A", "B", "B", "C", "C", "D")

The idea is that if certain value has duplicates then drop all duplicate values and the value itself. In order to get something that looks like this:

# A  D

Is there any way to get the above output?

AlSub
  • 1,384
  • 1
  • 14
  • 33

1 Answers1

0

Using duplicated in forward and reverse to return two logical vectors, then use OR (|) when either one of them is TRUE, negate (!) and subset the vector

just_a_random_vector[!(duplicated(just_a_random_vector)|
        duplicated(just_a_random_vector, fromLast = TRUE))]
[1] "A" "D"

Or another option is table to create a logical vector based on the frequency count i.e. count equal to 1 is returned

just_a_random_vector [just_a_random_vector %in%
          names(which(table(just_a_random_vector) == 1))]
[1] "A" "D"
akrun
  • 874,273
  • 37
  • 540
  • 662