Open In App

Difference between two vectors in R

Last Updated : 23 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Method 1: Using setdiff() method

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 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)


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 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)


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 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)


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

Method 2: Using %in% operator

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 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)


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 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)


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

Method 3: Using intersect method

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 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)


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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads