如何判断一个向量中有什么而不是另一个向量中有什么?

在 matlab 中,有一种方法可以找到一个向量中的值,但不能找到另一个向量中的值。

例如:

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

有没有一个函数能告诉我,在 x中不在 y中的值是1?

83894 次浏览

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.

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

> setdiff(x, y)
[1] 1

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
x[is.na(match(x,y))]

setdiff() is a tricky function because the output is dependent on the order of the input. You can instead write a simple function as such that does the exact opposite of intersect. This is far better.

>difference <- function(x, y) {
c(setdiff(x, y), setdiff(y, x))
}


#Now lets test it.
>x <- c(1,2,3,4)
>y <- c(2,3,4,5)


>difference(x,y)
[1] 1 5

If:

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

Any of these expressions:

setdiff(x, y)
x[!(x %in% y)]
x[is.na(match(x,y))]
x[!(is.element(x,y))]

will give you the right answer [1] 1, if the goal is to find the values/characters in x, that is not present in y.

However, applying the above expressions can be tricky and can give undesirable results depending on the nature of the vector, and the position of x and y in the expression. For instance, if:

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

and the goal is just to find the unique values/characters in x, that is not present in y or vice-versa. Applying any of these expressions will still give the right answer [1] 1:

union(setdiff(x, y), setdiff(y, x))

Thanks to contribution of Jeromy Anglim

OR:

difference <- function(x, y) {
c(setdiff(x, y), setdiff(y, x))
}
difference(y,x)

Thanks to contribution of Workhouse