从R中的字符串向量中提取非相交元素
Extract non intersected element from a string vector in R
我有 2 个字符串向量,我将它们相交以获得相交的字符串向量,但现在,我需要得到另外 2 个没有相交元素的字符串向量。
即 vector.1
没有 intersected.vector
元素,vector.2
没有 intersected.vector
元素。我该怎么做?
我希望这个简单的例子能说明您的要求:
vector.1 <- LETTERS[1:4]
vector.2 <- LETTERS[3:6]
> vector.1
[1] "A" "B" "C" "D"
> vector.2
[1] "C" "D" "E" "F"
intersected.vector <- intersect(vector.1, vector.2)
> intersected.vector
[1] "C" "D"
new_vec1 <- vector.1[-which(vector.1 %in% intersected.vector)]
> new_vec1
[1] "A" "B"
new_vec2 <- vector.2[-which(vector.2 %in% intersected.vector)]
> new_vec2
[1] "E" "F"
我有 2 个字符串向量,我将它们相交以获得相交的字符串向量,但现在,我需要得到另外 2 个没有相交元素的字符串向量。
即 vector.1
没有 intersected.vector
元素,vector.2
没有 intersected.vector
元素。我该怎么做?
我希望这个简单的例子能说明您的要求:
vector.1 <- LETTERS[1:4]
vector.2 <- LETTERS[3:6]
> vector.1
[1] "A" "B" "C" "D"
> vector.2
[1] "C" "D" "E" "F"
intersected.vector <- intersect(vector.1, vector.2)
> intersected.vector
[1] "C" "D"
new_vec1 <- vector.1[-which(vector.1 %in% intersected.vector)]
> new_vec1
[1] "A" "B"
new_vec2 <- vector.2[-which(vector.2 %in% intersected.vector)]
> new_vec2
[1] "E" "F"