2009-12-03 77 views

回答

83

可以使用setdiff()(设定差)函数:

> setdiff(x, y) 
[1] 1 
+14

WATCHOUT:'setdiff(X,Y)'和'setdiff(Y,X)'是不一样的。 – 2017-02-21 08:36:25

44

是的。对于载体,您可以简单地使用%in%运算符或is.element()函数。

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

对于一个矩阵,有很多不同的方法。 merge()可能是最直接的。我建议looking at this question for that scenario

20

R中的帮助文件中setdiff, union, intersect, setequal, and is.element R.提供的标准集的功能的信息

setdiff(x, y)返回的元素x不在y

如上所述,这是一个不对称的区别。 因此,例如:

> 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 
+0

几乎没有信息。 – ddunn801 2017-10-05 18:58:25

+1

@ ddunn801我认为很高兴知道一般帮助文件,但我添加了一些更多的信息,使其更有用。 – 2017-10-05 22:57:10

12
x[is.na(match(x,y))] 
相关问题