Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding elements in a vector that are duplicated or that are not in another vector

Tags:

r

match

I have the following situation:

vec1  <- c("A", "B", "D", "C", "E", "A", "C")
vec2 <- c("A", "B", "C", "D", "F")

First question: which one is duplicated ? - answer "A" and "C" for vec1, 0 for vec2

Second question: Identify which is vec1 but not in vec2, irrespective of order (answer "E")

or vice versa (answer "F")

which(vec1 !=vec2)
which(vec2 !=vec1)

[1] 3 4 5 7
Warning message:
In vec1 != vec2 :
  longer object length is not a multiple of shorter object length

which is not what I expected....

like image 858
fprd Avatar asked Dec 27 '22 22:12

fprd


1 Answers

For the first question, try ?duplicated

vec1.dup <- duplicated(vec1)
unique(vec1[vec1.dup])

[1] "A" "C"

For the second, try ?setdiff. You want the values of vec2 that are not in vec1.

setdiff(vec2, vec1)
[1] "F"
like image 92
sebastian-c Avatar answered Jan 30 '23 22:01

sebastian-c