我有以下情况:
vec1 <- c("A", "B", "D", "C", "E", "A", "C")
vec2 <- c("A", "B", "C", "D", "F")
第一个问题:哪一个是重复的? - 回答vec1的“A”和“C”,vec2的“0”
第二个问题:确定哪个是vec1而不是vec2,无论顺序如何(回答“E”)
反之亦然(回答“F”)
which(vec1 !=vec2)
which(vec2 !=vec1)
[1] 3 4 5 7
Warning message:
In vec1 != vec2 :
longer object length is not a multiple of shorter object length
这不是我的预期......
答案 0 :(得分:4)
对于第一个问题,请尝试?duplicated
vec1.dup <- duplicated(vec1)
unique(vec1[vec1.dup])
[1] "A" "C"
对于第二个,请尝试?setdiff
。你想要vec2的值不在vec1。
setdiff(vec2, vec1)
[1] "F"
答案 1 :(得分:3)
vec1中重复的元素:
vec1[duplicated(vec1)]
[1] "A" "C"
vec1中不在vec2中的元素:
vec1[is.na(match(vec1,vec2))]
[1] "E"
反之亦然:
vec2[is.na(match(vec1,vec2))]
[1] "F"
答案 2 :(得分:3)
看来你的(第二个)问题是..为什么? (我确实看到你得到了正确的答案......怎么样?)
which(vec1 !=vec2)
which(vec2 !=vec1)
两者都返回
[1] 3 4 5 7
答案主要在于您未包含的警告信息:
Warning message:
In vec1 != vec2 :
longer object length is not a multiple of shorter object length
当像“!=”这样的二元运算符处理向量时,回收规则会接管,因此两个向量中较长的一个确定比较的“范围”,而较短的向量则通过回收扩展。你最终测试了:
> c("A", "B", "C", "D", "F", "A", "B") != c("A", "B", "D", "C", "E", "A", "C")
#.... extending shorter one ^^^^^^^
[1] FALSE FALSE TRUE TRUE TRUE FALSE TRUE
> c("A", "B", "D", "C", "E","A", "C") != c("A", "B", "C", "D", "F", "A", "B")
#.... extending shorter one ^^^^^^^
[1] FALSE FALSE TRUE TRUE TRUE FALSE TRUE