# all.equal Function in R (2 Examples)

In this R programming tutorial youâ€™ll learn how to **test if two objects are nearly equal using the all.equal function**.

The article contains the following information:

Hereâ€™s how to do itâ€¦

## Example Data

Weâ€™ll use the following data as basement for this R programming tutorial:

x <- 1:10 # Create first vector object x # Print first vector object # [1] 1 2 3 4 5 6 7 8 9 10

y1 <- 1:10 # Create second vector object y1 # Print second vector object # [1] 1 2 3 4 5 6 7 8 9 10

set.seed(325967) # Set random seed y2 <- 1:10 + rnorm(10, 0, 0.1) # Create third vector object y2 # Print third vector object # [1] 1.125965 1.827091 3.042830 3.943630 4.863133 6.063811 6.960500 8.013362 # [9] 9.057638 9.931102

Have a look at the previous outputs of the RStudio console. It shows the content of our three example vectors. The vectors x and y1 are identical, and the vector y2 is slightly different.

Letâ€™s compare these vectors using the all.equal function!

## Example 1: Basic Application of all.equal() Function

The following R programming code demonstrates how to apply the all.equal function with default specifications.

For this, we simply have to specify the two data objects that we want to compare within the all.equal function:

all.equal(x, y1) # Identical vectors # [1] TRUE

The RStudio console has returned TRUE, i.e. the vector objects x and y1 are identical.

Now, letâ€™s apply the all.equal function to the vector objects x and y2:

all.equal(x, y2) # Similar vectors # [1] "Mean relative difference: 0.0141482"

The all.equal function has returned the mean relative difference between our two vector objects, i.e. 0.0141482.

In other words: the vector objects x and y2 are not identical.

## Example 2: all.equal() Function with Tolerance

A useful feature of the all.equal function is that the user can specify a tolerance threshold. Differences smaller than this tolerance are not reported by the all.equal function.

In the following R code, we specify a tolerance of 0.1:

all.equal(x, y2, # Similar vectors with increased tolerance tolerance = 0.1) # [1] TRUE

As you can see, the all.equal function has returned TRUE, even though our two vectors are slightly different.

## Video, Further Resources & Summary

In case you need further info on the R programming codes of this article, I recommend watching the following video on my YouTube channel. In the video, Iâ€™m explaining the R codes of this article.

Furthermore, you might read the other tutorials on my website. Some interesting posts are shown below:

- all_equal Function of dplyr R Package
- Test for Equality of All Vector Elements
- Compare Vectors and Find Differences
- Compare Character Strings with Logical Operator Conditionally
- identical Function in R
- R Commands List (Example Codes)
- R Programming Examples

In summary: In this R programming tutorial you have learned how to **apply the all.equal function**. If you have further questions, let me know in the comments.