【发布时间】:2012-10-28 06:09:16
【问题描述】:
假设我有两个向量:
x <- c(1,16,20,7,2)
y <- c(1, 7, 5,2,4,16,20,10)
我想删除y 中不在x 中的元素。也就是说,我想从y 中删除元素5, 4, 10。
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
有人知道怎么做吗?谢谢
【问题讨论】: