Difference between two vectors in R - GeeksforGeeks

23 May, 2021
In this article, we will see how to find the difference between two vectors in R Programming Language. 
The difference (A-B) between two vectors in R Programming is equivalent to the elements present in A which are not present in B. The resultant elements are always a subset of A. In case, both sets are non-intersecting, the entire A set is returned. 
The setdiff() method in R is used to retrieve the elements of vector X, which are not contained in Y. This method can be applied where the two vectors may belong to different data types, as well, where the elements of the first argument vector are returned unmodified. In case, the input vectors are equivalent, that is, they contain the same elements, then the resultant vector will have null entries and is referred by the datatype(0) output. Also, different types of results is obtained upon changing the order of vectors during the function call. 
Syntax:
setdiff( X, Y)
Example:
R
# declaring first integer vectorvec1 <- c(1:5)  # declaring second string vectorvec2 <- c(4:8)  print ("Original vector1 ")print (vec1)  print ("Original vector2 ")print (vec2)  # computing the difference # in vectorsdiff <- setdiff(vec1,vec2)print ("Vec1- Vec2")print (diff)
Output
[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
This method works for string vectors as well. 
Example:
R
# declaring first integer vectorvec1 <- c("Geeksforgeeks","Interviews","Science")  # declaring second string vectorvec2 <- c("Algorithms","Science",          "placements","data structures")  print ("Original vector1 ")print (vec1)  print ("Original vector2 ")print (vec2)  # computing the difference in # vectorsdiff <- setdiff(vec2,vec1)print ("Vec2 - Vec1")print (diff)
Output
[1] “Original vector1 “
[1] “Geeksforgeeks” “Interviews”    “Science”      
[1] “Original vector2 “
[1] “Algorithms”      “Science”         “placements”      “data structures”
[1] “Vec2 – Vec1”
[1] “Algorithms”      “placements”      “data structures”
Also, this method automatically returns unique elements of the resultant vector. Any duplicate elements are removed. 
Example:
R
# declaring first integer vectorvec1 <- c("Geeksforgeeks","Interviews","Science")  # declaring second string vectorvec2 <- c(1,2,3,5,5)  print ("Original vector1 ")print (vec1)  print ("Original vector2 ")print (vec2)  # computing the difference in vectorsdiff <- setdiff(vec2,vec1)print ("Vec2 - Vec1")print (diff)
Output
[1] "Original vector1 "
[1] "Geeksforgeeks" "Interviews"    "Science"      
[1] "Original vector2 "
[1] 1 2 3 5 5
[1] "Vec2 - Vec1"
[1] 1 2 3 5
The %in% operator can be used to check for the presence of an element in the list. This approach first checks which indexes of vector1 are not in vector2 and then the corresponding elements of vector1 are returned. This is followed by the application of the unique() method, which returns only unique elements of the resultant vector.
Syntax:
vec1[! vec1 %in% vec2]
Example:
R
# declaring first integer vectorvec1 <- c("Geeksforgeeks","Interviews","Science")  # declaring second string vectorvec2 <- c("Algorithms","Science",          "placements","data structures")  print ("Original vector1 ")print (vec1)  print ("Original vector2 ")print (vec2)  # computing the difference in vectorsdiff <- unique(vec1[! vec1 %in% vec2])print ("Vec1 - Vec2")print (diff)
Output
[1] “Original vector1 “
[1] “Geeksforgeeks” “Interviews”    “Science”      
[1] “Original vector2 “
[1] “Algorithms”      “Science”         “placements”      “data structures”
[1] “Vec1 – Vec2”
[1] “Geeksforgeeks” “Interviews”   
This approach is also compatible with vectors belonging to different data types. In this case, the elements of the vec1 are returned. 
Example:
R
# declaring first integer vectorvec1 <- c("Geeksforgeeks","Interviews","Science")  # declaring second string vectorvec2 <- c(1,2,3,5,5)  print ("Original vector1 ")print (vec1)  print ("Original vector2 ")print (vec2)  # computing the difference in vectorsdiff <- unique(vec2[! vec2 %in% vec1])print ("Vec2 - Vec1")print (diff)
Output
[1] "Original vector1 "
[1] "Geeksforgeeks" "Interviews"    "Science"      
[1] "Original vector2 "
[1] 1 2 3 5 5
[1] "Vec2 - Vec1"
[1] 1 2 3 5
The intersect() method in Base R is used to calculate the intersection of elements in the specified argument vectors. It returns a vector array of all the elements present in both the input vectors. The approach involves two steps, first is, intersect() method to return the intersection array. Next, is the application of negation of %in% operator to get elements of the first vector that are not present in the intersection. The returned elements will be contained in the only the first vector. 
Example:
R
# declaring first integer vectorvec1 <- c(1:5)  # declaring second string vectorvec2 <- c(4:8)  print ("Original vector1 ")print (vec1)  print ("Original vector2 ")print (vec2)  # computing the intersection of two # vectorsintersect <- intersect(vec1,vec2)  # getting elements of vec1 not in # intersectiondiff <- vec1[!(vec1 %in% intersect)]print ("Elements of vec1 not in vec2")print(diff)
Output
[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
Picked
R Vector-Programs
R-Vectors
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Convert Matrix to Dataframe in R