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:

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

[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 –

- R – Check If All Elements in a Vector are Equal
- R – Count Distinct Values in a Vector
- Remove Duplicates From a Vector in R

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