Compare Vectors and Find Differences in R (5 Examples)

 

This page illustrates how to identify similarities and differences between two vector objects in the R programming language.

Table of contents:

Let’s do this…

 

Example Data

Consider the following first example vector:

vec1 <- c("A", "B", "C")        # Create first vector
vec1                            # Print first vector
# [1] "A" "B" "C"

The previous output of the RStudio console shows that our first example vector has three vector elements: The characters A, B, and C.

Let’s create another example vector:

vec2 <- c("A", "B", "D")        # Create second vector
vec2                            # Print first vector
# [1] "A" "B" "D"

Our second example vector also contains three elements. However, the second vector consists of the characters A, B, and D.

In the following examples, I’ll show different alternatives on how to compare these two vectors in R.

 

Example 1: Check If Two Vectors are Exactly the Same Using identical() Function

In Example 1, I’ll illustrate how to test whether our two vectors are exactly the same, i.e. if each vector element of the first vector is equal to the corresponding vector element in the second vector.

For this, we can use the identical function as shown below:

identical(vec1, vec2)           # Apply identical function
# [1] FALSE

The RStudio console returns the logical value FALSE, i.e. our two vectors are not identical.

 

Example 2: Check Which Vector Elements of Two Vectors are the Same Using == Operator

This example explains how to test for equality element by element using the == operator.

vec1 == vec2                    # Apply == operator
# [1]  TRUE  TRUE FALSE

The first and the second element are the same, but the third element is different.

Note that we can apply this R code only in case that both vectors have the same length (or in case the length of one vector is a multiplier of the length of the other vector).

This condition does not have to be fulfilled in the next example…

 

Example 3: Check Which Elements of First Vector Exist in Second Vector Using %in% Operator

In this example, I’ll use the %in% operator to test whether the elements of the first vector are equal to any element of the second vector.

vec1 %in% vec2                  # Apply %in% operator
# [1]  TRUE  TRUE FALSE

Again, the RStudio console returns TRUE for the first and the second vector elements.

 

Example 4: Find Elements that Exist in First & Second Vector Using intersect() Function

The syntax below shows how to return all vector elements that exist in both of our vectors using the intersect function.

intersect(vec1, vec2)           # Apply intersect function
# [1] "A" "B"

The characters A and B exist in both arrays.

 

Example 5: Find Elements that Exist Only in First, But Not in Second Vector Using setdiff() Function

In Example 5, I’ll illustrate how to apply the setdiff function to return values that only exist in the first vector.

setdiff(vec1, vec2)             # Apply setdiff function
# [1] "C"

The letter C exists only in the first vector.

Note that the ordering of our vectors within the setdiff function matters. The setdiff function returns only values that exist in the first input vector.

 

Video, Further Resources & Summary

Some time ago I have released a video on my YouTube channel, which illustrates the topics of this article. Please find the video below.

 

The YouTube video will be added soon.

 

Besides the video, you may have a look at the related RStudio posts on Statistics Globe.

 

To summarize: At this point you should have learned how to compare vectors in the R programming language.

Note that we could use the same R syntax to compare columns in a data frame as well.

Please let me know in the comments section, in case you have any additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top