2009-12-03 76 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))] 
相關問題