9

R 中是否有函数可以在多个向量(不同长度)中查找公共字符。例如,如果我有 3 个向量...

 a1 <- LETTERS[1:7]
 a2 <- LETTERS[4:8]
 a3 <- LETTERS[2:10]

a1
# [1] "A" "B" "C" "D" "E" "F" "G"
a2
# [1] "D" "E" "F" "G" "H"
a3
# [1] "B" "C" "D" "E" "F" "G" "H" "I" "J"

我能想到一个混乱的解决方案......

intersect(intersect(a1,a2),a3)
# [1] "D" "E" "F" "G"

问题是,我有大约 8 或 9 个向量。有更好的方法吗?

4

1 回答 1

8

是的:

Reduce(intersect,list(a1,a2,a3))
于 2012-05-30T18:29:52.003 回答