#Compare Vectors and Find Differences in R (5 Examples)
vec1 <- c("A", "B", "C") # Create first vector
vec1 # Print first vector
## [1] "A" "B" "C"
vec2 <- c("A", "B", "D") # Create second vector
vec2 # Print first vector
## [1] "A" "B" "D"
#Example 1: Check If Two Vectors are Exactly the Same Using identical() Function
identical(vec1, vec2) # Apply identical function
## [1] FALSE
#Example 2: Check Which Vector Elements of Two Vectors are the Same Using == Operator
vec1 == vec2 # Apply == operator
## [1] TRUE TRUE FALSE
#Example 3: Check Which Elements of First Vector Exist in Second Vector Using %in% Operator
vec1 %in% vec2
## [1] TRUE TRUE FALSE
c("A", "B", "C") %in% c("A", "B", "D","C")
## [1] TRUE TRUE TRUE
c("A", "B", "D","C") %in% c("A", "B", "C")
## [1] TRUE TRUE FALSE TRUE
#Example 4: Find Elements that Exist in First & Second Vector Using intersect() Function
intersect(vec1, vec2) # Apply intersect function
## [1] "A" "B"
#Example 5: Find Elements that Exist Only in First, But Not in Second Vector Using setdiff() Function
setdiff(vec1, vec2)
## [1] "C"
#ref https://statisticsglobe.com/compare-vectors-and-find-differences-in-r