Difference between two vectors in R
Last Updated :
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.
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
Similar Reads
Compare two character vectors in R In this article, we will discuss how to compare two character vectors in R Programming Language. Method 1: Using %in% This operator is used to find the elements present in one vector with respect to the second vector Syntax: vector1 %in% vector2 Return type: It returns boolean values corresponding t
2 min read
How to combine two vectors in R ? In this article, we will learn how to combine two vectors in R Programming Language. We can combine two or more vectors using function c() itself. While using function c() All arguments are coerced to a common type which is the type of the returned value. Syntax: c(...) Parameters: â¦: arguments to b
3 min read
How to Print a Vector in R In this article, we'll look at how to print a vector's elements in R using a for loop. We'll go through a for loop's fundamental syntax, describe how to access vector members inside the loop, and give instances of several situations in which this method might be helpful. Learning how to use for loop
3 min read
How to Filter a Vector in R In this article, we are going to discuss how to filter a vector in the R programming language. Filtering a vector means getting the values from the vector by removing the others, we can also say that getting the required elements is known as filtering. Method 1: Using %in% Here we can filter the ele
2 min read
Convert List of Vectors to DataFrame in R In this article, we will discuss how to convert the given list of vectors to Dataframe using the help of different functions in the R Programming Language. For all the methods discussed below, few functions are common because of their functionality. Let us discuss them first. as.data.frame() is one
2 min read
Convert Named Vector to DataFrame in R In this article, we will see how to convert the named vector to Dataframe in the R Programming Language. Method 1: Generally while converting a named vector to a dataframe we may face a problem. That is, names of vectors may get converted into row names, and data may be converted into a single colu
1 min read
How to create an array from vectors in R ? In this article, we will discuss how to create an array from the vectors in R Programming Language. We can create an array using array() function. We have to pass the vectors and dim() as parameters. Here dim() function is used to give dimensions for the array. Syntax: array_name = array( c(vector 1
2 min read
Calculate difference between dataframe rows by group in R In this article, we will see how to find the difference between rows by the group in dataframe in R programming language. Method 1: Using dplyr package The group_by method is used to divide and segregate date based on groups contained within the specific columns. The required column to group by is s
5 min read
How to concatenate two or more vectors in R ? In this article, we will see how to concatenate two or more vectors in R Programming Language. To concatenate two or more vectors in r we can use the combination function in R. Let's assume we have 3 vectors vec1, vec2, vec3 the concatenation of these vectors can be done as c(vec1, vec2, vec3). Als
2 min read