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:

 

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.

 

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.

Top