假设我有两个向量:
x <- c(1,16,20,7,2)
y <- c(1, 7, 5,2,4,16,20,10)
我想删除y
不在x
. 也就是说,我想5, 4, 10
从y
.
y
[1] 1 7 2 16 20
最后,我想要向量x
并且y
必须拥有相同的元素。顺序无所谓。
我的想法:该match
函数列出了两个向量包含匹配元素的索引,但我需要一个基本上相反的函数。我需要一个函数来显示两个向量中的元素不匹配的索引。
# this lists the indices in y that match the elements in x
match(x,y)
[1] 1 6 7 2 4 # these are the indices that I want; I want to remove
# the other indices from y
有谁知道如何做到这一点?谢谢你