0

I'm trying to extract pure unique values in R.

For example:

vec <- c("a", "b", "c","c") 

Using duplicate() I get:

vec[!duplicated(vec, fromLast=TRUE)]
[1] "a" "b" "c"

But I want the pure unique values, so only "a" and "b".

Using unique() I get the same output.

Anyone know how to solve this?

1 Answers1

1

You can use the following code with ave which counts the length of unique values and takes only the elements whose length is 1 to skip the duplicates:

vec <- c("a", "b", "c","c") 
vec[ave(vec, vec, FUN = length) == 1]

Output:

[1] "a" "b"
Quinten
  • 35,235
  • 5
  • 20
  • 53