tags:

views:

225

answers:

4

In matlab there is a way to find the values in one vector but not in the other.

for example:

x <- c(1,2,3,4)
y <- c(2,3,4)

is there any function that would tell me that the value in x that's not in y is 1?

+3  A: 

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.

Shane
+10  A: 

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

> setdiff(x, y)
[1] 1
Xela
+2  A: 

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

Jeromy Anglim
+1  A: 
x[is.na(match(x,y))]
gd047