如何找到至少2个向量中常见的元素? [英] How to find elements common in at least 2 vectors?
问题描述
假设我有 5 个向量:
Say I have 5 vectors:
a <- c(1,2,3)
b <- c(2,3,4)
c <- c(1,2,5,8)
d <- c(2,3,4,6)
e <- c(2,7,8,9)
我知道我可以通过使用 Reduce()
和 intersect()
来计算它们之间的交集,就像这样:
I know I can calculate the intersection between all of them by using Reduce()
together with intersect()
, like this:
Reduce(intersect, list(a, b, c, d, e))
[1] 2
但是我怎样才能找到在至少 2 个向量中常见的元素?即:
But how can I find elements that are common in, say, at least 2 vectors? i.e.:
[1] 1 2 3 4 8
推荐答案
它比很多人想象的要简单得多.这应该是非常有效的.
It is much simpler than a lot of people are making it look. This should be very efficient.
将所有内容放入向量中:
Put everything into a vector:
x <- unlist(list(a, b, c, d, e))
查找重复项
Look for duplicates
unique(x[duplicated(x)])
# [1] 2 3 1 4 8
和 sort
(如果需要).
注意:如果列表元素中可能存在重复项(您的示例似乎没有暗示),请将 x
替换为 x <- unlist(lapply(list(a, b, c, d, e), 唯一))
Note: In case there can be duplicates within a list element (which your example does not seem to implicate), then replace x
with x <- unlist(lapply(list(a, b, c, d, e), unique))
由于 OP 表示对 n >= 2 的更通用解决方案感兴趣,我会这样做:
as the OP has expressed interest in a more general solution where n >= 2, I would do:
which(tabulate(x) >= n)
如果数据仅由示例中的自然整数(1、2 等)组成.如果不是:
if the data is only made of natural integers (1, 2, etc.) as in the example. If not:
f <- table(x)
names(f)[f >= n]
这现在离 James 解决方案不远了,但它避免了昂贵的 sort
.它比计算所有可能的组合要快几英里.
This is now not too far from James solution but it avoids the costly-ish sort
. And it is miles faster than computing all possible combinations.
这篇关于如何找到至少2个向量中常见的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!