Skip to Content

Compare Two Vectors For Equality in R

Vectors are used to store one-dimensional data of the same type in R. In this tutorial, we will look at how to compare two vectors for equality in R with the help of some examples.

How to check if two vectors are equal in R?

You can use the identical() function in R to compare two vectors for equality. Pass the two vectors as arguments to the indentical() function.

The following is the syntax –

identical(vec1, vec2)

It returns TRUE if both vectors contain the same elements in the same positions. That is, the corresponding elements in both vectors are the same.

Alternatively, you can use a combination of the all() function and the equality operator to check whether the two vectors are equal or not. The following is the syntax –

all(vec1 == vec2)

Let’s now look at some examples of using the above methods to check two vectors for equality.

Compare two vectors for equality using the identical() function

Let’s create two vectors containing the same values at the same positions and check whether they are equal or not using the identical() function.

# create two vector
vec1 <- c(1, 2, 3, 4)
vec2 <- c(1, 2, 3, 4)
# check if both vectors are equal
print(identical(vec1, vec2))

Output:

[1] TRUE

We get TRUE as the output since both vectors are equal.

Let’s now apply the identical() function on two vectors that are not equal.

# create two vector
vec1 <- c(1, 2, 3, 4)
vec2 <- c(1, 3, 3, 4)
# check if both vectors are equal
print(identical(vec1, vec2))

Output:

[1] FALSE

We get FALSE as the output.

Compare two vectors for equality using the all() function and the == operator

You can also use a combination of the R all() function and the equality operator == to check whether two vectors are equal or not.

First, use the == operator to compare the two vectors, this will result in a logical vector with TRUE for values that are equal and FALSE for values that are not equal.

# create two vector
vec1 <- c(1, 2, 3, 4)
vec2 <- c(1, 2, 3, 4)
# compare vec1 and vec2 
print(vec1 == vec2)

Output:

[1] TRUE TRUE TRUE TRUE

If all the values in the resulting logical vector are TRUE then we can say that both the vectors are equal. You can do so using the R all() function which returns TRUE only if all the values in the passed logical vector are TRUE.

# check if both vectors are equal
print(all(vec1 == vec2))

Output:

[1] TRUE

Thus, a combination of the equality operator, == and the all() function can tell whether two vectors are equal.

Let’s look at another example.

# create two vector
vec1 <- c(1, 2, 3, 4)
vec2 <- c(1, 3, 3, 4)
# check if both vectors are equal
print(all(vec1 == vec2))

Output:

[1] FALSE

We get FALSE as the output since the two vectors are not equal.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.