不同载体中的常见和独特元素

时间:2015-09-21 21:06:11

标签: r

我有两个字符串向量,我想返回它们共有的内容以及向量a和向量b的唯一内容。

Vector

2 个答案:

答案 0 :(得分:3)

正如@David Arenburg的评论中已经提到的,您可以在intersect中使用setdiffbase - R:

> a<- letters[seq( from = 1, to = 10 )]
> b<- letters[seq( from = 6, to = 15 )]
> a
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> b
 [1] "f" "g" "h" "i" "j" "k" "l" "m" "n" "o"
> intersect(a, b)
[1] "f" "g" "h" "i" "j"
> setdiff(a, b)
[1] "a" "b" "c" "d" "e"
> setdiff(b, a)
[1] "k" "l" "m" "n" "o"

答案 1 :(得分:2)

共同要素:

a[a %in% b] # or b[b %in% a]
[1] "f" "g" "h" "i" "j"

独一无二:

a[!a %in% b] # or a[b %in% a]
[1] "a" "b" "c" "d" "e"

b独有:

b[!b %in% a] # or b[a %in% b]
[1] "k" "l" "m" "n" "o"
  

备注

值得指出的是,intersectsetdiff将丢弃参数中的任何重复值。而%in%将保留重复。因此,如果我们有重复的值,我们将得到不同的结果。例如,在向量a:

中生成重复元素 a
a <- c(a, "a")
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "a"
# Duplicated element a is discarded
setdiff(a, b)
[1] "a" "b" "c" "d" "e"
# Keeps duplicated element a
a[!a %in% b]
[1] "a" "b" "c" "d" "e" "a"