我有两个字符串向量,我想返回它们共有的内容以及向量a和向量b的唯一内容。
Vector
答案 0 :(得分:3)
正如@David Arenburg的评论中已经提到的,您可以在intersect
中使用setdiff
和base
- 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"
备注强>
值得指出的是,intersect
和setdiff
将丢弃参数中的任何重复值。而%in%
将保留重复。因此,如果我们有重复的值,我们将得到不同的结果。例如,在向量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"