R中两个向量之间的差异
在本文中,我们将看到如何在 R 编程语言中找到两个向量之间的差异。
R 编程中两个向量之间的差(AB)相当于 A 中存在但 B 中不存在的元素。结果元素始终是 A 的子集。如果两个集合不相交,则整个 A 集被退回。
方法一:使用setdiff()方法
R 中的 setdiff() 方法用于检索向量 X 中不包含在 Y 中的元素。此方法可以应用于两个向量可能属于不同数据类型的情况,以及第一个参数的元素返回未修改的向量。如果输入向量是等效的,即它们包含相同的元素,则结果向量将具有空条目并由 datatype(0) 输出引用。此外,在函数调用期间改变向量的顺序会获得不同类型的结果。
句法:
setdiff( X, Y)
例子:
R
# declaring first integer vector
vec1 <- c(1:5)
# declaring second string vector
vec2 <- c(4:8)
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference
# in vectors
diff <- setdiff(vec1,vec2)
print ("Vec1- Vec2")
print (diff)
R
# declaring first integer vector
vec1 <- c("Geeksforgeeks","Interviews","Science")
# declaring second string vector
vec2 <- c("Algorithms","Science",
"placements","data structures")
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference in
# vectors
diff <- setdiff(vec2,vec1)
print ("Vec2 - Vec1")
print (diff)
R
# declaring first integer vector
vec1 <- c("Geeksforgeeks","Interviews","Science")
# declaring second string vector
vec2 <- c(1,2,3,5,5)
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference in vectors
diff <- setdiff(vec2,vec1)
print ("Vec2 - Vec1")
print (diff)
R
# declaring first integer vector
vec1 <- c("Geeksforgeeks","Interviews","Science")
# declaring second string vector
vec2 <- c("Algorithms","Science",
"placements","data structures")
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference in vectors
diff <- unique(vec1[! vec1 %in% vec2])
print ("Vec1 - Vec2")
print (diff)
R
# declaring first integer vector
vec1 <- c("Geeksforgeeks","Interviews","Science")
# declaring second string vector
vec2 <- c(1,2,3,5,5)
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference in vectors
diff <- unique(vec2[! vec2 %in% vec1])
print ("Vec2 - Vec1")
print (diff)
R
# declaring first integer vector
vec1 <- c(1:5)
# declaring second string vector
vec2 <- c(4:8)
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the intersection of two
# vectors
intersect <- intersect(vec1,vec2)
# getting elements of vec1 not in
# intersection
diff <- vec1[!(vec1 %in% intersect)]
print ("Elements of vec1 not in vec2")
print(diff)
输出
[1] "Original vector1 "
[1] 1 2 3 4 5
[1] "Original vector2 "
[1] 4 5 6 7 8
[1] "Vec1- Vec2"
[1] 1 2 3
此方法也适用于字符串向量。
例子:
电阻
# declaring first integer vector
vec1 <- c("Geeksforgeeks","Interviews","Science")
# declaring second string vector
vec2 <- c("Algorithms","Science",
"placements","data structures")
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference in
# vectors
diff <- setdiff(vec2,vec1)
print ("Vec2 - Vec1")
print (diff)
输出
[1] “Original vector1 “
[1] “Geeksforgeeks” “Interviews” “Science”
[1] “Original vector2 “
[1] “Algorithms” “Science” “placements” “data structures”
[1] “Vec2 – Vec1”
[1] “Algorithms” “placements” “data structures”
此外,此方法会自动返回结果向量的唯一元素。删除任何重复的元素。
例子:
电阻
# declaring first integer vector
vec1 <- c("Geeksforgeeks","Interviews","Science")
# declaring second string vector
vec2 <- c(1,2,3,5,5)
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference in vectors
diff <- setdiff(vec2,vec1)
print ("Vec2 - Vec1")
print (diff)
输出
[1] "Original vector1 "
[1] "Geeksforgeeks" "Interviews" "Science"
[1] "Original vector2 "
[1] 1 2 3 5 5
[1] "Vec2 - Vec1"
[1] 1 2 3 5
方法 2:使用 %in%运算符
%in%运算符可用于检查列表中是否存在元素。这种方法首先检查 vector1 的哪些索引不在 vector2 中,然后返回 vector1 的相应元素。接着应用 unique() 方法,该方法仅返回结果向量的唯一元素。
句法:
vec1[! vec1 %in% vec2]
例子:
电阻
# declaring first integer vector
vec1 <- c("Geeksforgeeks","Interviews","Science")
# declaring second string vector
vec2 <- c("Algorithms","Science",
"placements","data structures")
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference in vectors
diff <- unique(vec1[! vec1 %in% vec2])
print ("Vec1 - Vec2")
print (diff)
输出
[1] “Original vector1 “
[1] “Geeksforgeeks” “Interviews” “Science”
[1] “Original vector2 “
[1] “Algorithms” “Science” “placements” “data structures”
[1] “Vec1 – Vec2”
[1] “Geeksforgeeks” “Interviews”
这种方法也与属于不同数据类型的向量兼容。在这种情况下,将返回 vec1 的元素。
例子:
电阻
# declaring first integer vector
vec1 <- c("Geeksforgeeks","Interviews","Science")
# declaring second string vector
vec2 <- c(1,2,3,5,5)
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the difference in vectors
diff <- unique(vec2[! vec2 %in% vec1])
print ("Vec2 - Vec1")
print (diff)
输出
[1] "Original vector1 "
[1] "Geeksforgeeks" "Interviews" "Science"
[1] "Original vector2 "
[1] 1 2 3 5 5
[1] "Vec2 - Vec1"
[1] 1 2 3 5
方法3:使用intersect方法
Base R 中的 intersect() 方法用于计算指定参数向量中元素的交集。它返回两个输入向量中存在的所有元素的向量数组。该方法包括两个步骤,首先是 intersect() 方法返回交集数组。接下来,是应用 %in%运算符的否定来获取交集中不存在的第一个向量的元素。返回的元素将包含在唯一的第一个向量中。
Ëxample:
电阻
# declaring first integer vector
vec1 <- c(1:5)
# declaring second string vector
vec2 <- c(4:8)
print ("Original vector1 ")
print (vec1)
print ("Original vector2 ")
print (vec2)
# computing the intersection of two
# vectors
intersect <- intersect(vec1,vec2)
# getting elements of vec1 not in
# intersection
diff <- vec1[!(vec1 %in% intersect)]
print ("Elements of vec1 not in vec2")
print(diff)
输出
[1] "Original vector1 "
[1] 1 2 3 4 5
[1] "Original vector2 "
[1] 4 5 6 7 8
[1] "Elements of vec1 not in vec2"
[1] 1 2 3