Often you may want to check if all corresponding elements in two vectors are equal or roughly equal in R.

The easiest way to do so is by using the **all.equal()** function from base R, which is designed to perform this exact task.

The **all.equal****()** function uses the following basic syntax:

**all.equal(target, current, tolerance=1.5e=8, …)**

where:

**target**: Name of one vector**current**: Name of another vector to compare**tolerance**: Differences smaller than this number are not reported.

Note that you do not have to provide a value for the **tolerance** argument. The default value used is 1.5e-8.

This function will return one of the following values:

**TRUE**if all corresponding elements in each vector are equal (or less than the tolerance value)**The mean relative difference**if all corresponding elements in each vector are not equal

The following example shows how to use the **all.equal()** function in practice in several different scenarios.

**Note**: The **all.equal()** function comes built-in with base R so you do not need to install or load any external packages to use this function.

**Example: How to Use the all.equal() Function in R**

Suppose we create the following two vectors named **vector1** and **vector2**:

#create two vectors vector1 <- c(1, 2, 3, 4, 5, 6, 7, 8) vector2 <- c(1, 2, 3, 4, 5, 6, 7, 8)

We can use the **all.equal()** function to check if each of the corresponding elements from each of these vectors is equal:

#check if vectors are equal all.equal(vector1, vector2) [1] TRUE

Since each of the corresponding elements from each of these vectors are equal, the **all.equal()** function simply returns a value of **TRUE**.

Now suppose we create the following two vectors named **vector1** and **vector2** that do not have all of the same elements:

#create two vectors vector1 <- c(1, 2, 3, 4, 5) vector2 <- c(1, 2, 3, 4, 12)

We can use the **all.equal()** function to check if each of the corresponding elements from each of these vectors is equal:

#check if vectors are equal all.equal(vector1, vector2) [1] "Mean relative difference: 1.4"

Since each of the corresponding elements from each of these vectors are not equal, the **all.equal()** function returns the mean relative difference between the two vectors, which turns out to be **1.4**.

Note that the mean relative difference simply represents the difference between the mean of vector2 and vector1.

In this particular example, the mean of **vector2** is **4.4** while the mean of **vector1** is **3**.

Thus, the mean relative difference is calculated as **4.4 – 3 = 1.4**.

Note that we could specify a value for the **tolerance** argument if we’d like to specify an upper difference value that should be considered “equal” between the values.

For example, we can use the following syntax with the **all.equal()** function to check if all elements between two vectors are equal in which the **tolerance** value is set to **2**:

#create two vectors vector1 <- c(1, 2, 3, 4, 5) vector2 <- c(1, 2, 3, 4, 7)#check if vectors are equal all.equal(vector1, vector2, tolerance=2) [1] TRUE

This returns **TRUE** because there are no corresponding elements that have a difference greater than 2.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Use str_split in R

How to Use str_replace in R

How to Count Words in String in R

How to Convert a Vector to String in R