How to tell what is in one vector and not another?

you can use the setdiff() (set difference) function:

> setdiff(x, y)
[1] 1

Yes. For vectors you can simply use the %in% operator or is.element() function.

> x[!(x %in% y)]
1

For a matrix, there are many difference approaches. merge() is probably the most straight forward. I suggest looking at this question for that scenario.


The help file in R for setdiff, union, intersect, setequal, and is.element provides information on the standard set functions in R.

setdiff(x, y) returns the elements of x that are not in y.

As noted above, it is an asymmetric difference. So for example:

> x <- c(1,2,3,4)
> y <- c(2,3,4,5)
> 
> setdiff(x, y)
[1] 1
> setdiff(y, x)
[1] 5
> union(setdiff(x, y), setdiff(y, x))
[1] 1 5

Tags:

R

Vector