22

I have two character vectors, x and y.

x <- c("a", "b", "c", "d", "e", "f", "g")
y <- c("a",      "c", "d", "e",      "g")

The values inside x do not ever repeat (i.e., they are all unique). The same goes for vector y. My question is, how can I get R to compare the two vectors, and then tell me which elements are missing from y with respect to x? Otherwise stated, I want R to tell me that "b" and "f" are missing from y.

(Note, in my real data, x and y each contain a few thousand observations, which is why I would like to do this programmatically. There is likely a very simple answer, but I wasn't sure what to search for in the R help files).

Thanks to anyone who can help!

Alexander
  • 977
  • 1
  • 13
  • 29
  • 1
    This question is very similar to http://stackoverflow.com/questions/1837968/r-how-to-tell-what-is-in-one-vector-and-not-another – Ram Narasimhan Feb 06 '12 at 15:15
  • Sorry for the similar post! Thanks for linking to this and thank you to everyone who provided answers. – Alexander Feb 06 '12 at 15:20

3 Answers3

44
setdiff(x,y)

Will do the job for you.

  • 2
    It's worth noting that `setdiff(x,y)` is not the same as `setdiff(y,x)`. The former will tell you what's in `x` that's not in `y` whereas the latter will tell you what's in `y` that's not in `x`. – coip Feb 03 '21 at 23:54
12
> x[!x %in% y]
[1] "b" "f"

or:

> x[-match(y,x)]
[1] "b" "f"
> 
Justin
  • 42,475
  • 9
  • 93
  • 111
6

I think this should work:

x[!(x %in% y)]

First it checks for all x that are not in y, then it uses that as an index on the original.

nograpes
  • 18,623
  • 1
  • 44
  • 67