# Test if two objects are nearly equal in R

Test if two objects are nearly equal in R, Learn how to use them all.equal function to determine whether two items are nearly equal in this R programming tutorial.

This is how you do it:

## Example: Test if two objects are nearly equal in R

We’ll begin with the following information for the purposes of this R programming tutorial:

Data1<- 1:10 Data1

[1] 1 2 3 4 5 6 7 8 9 10

Data2 <- 1:10 Data2

[1] 1 2 3 4 5 6 7 8 9 10

set.seed(123) Data3 <- 1:10 + rnorm(10, 0, 0.1) Data3

[1] 0.9439524 1.9769823 3.1558708 4.0070508 5.0129288 6.1715065 7.0460916 7.8734939 8.9313147 9.9554338

Check out the RStudio console’s past outputs. It displays the information in the three example vectors. The vector Data2 differs somewhat from the vectors Data1 and Data2, which are identical.

Data Science applications in Retail » finnstats

Let’s use the all.equal method to compare these vectors!

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

The all.equal function can be used with default parameters by following the R programming code.

To do this, we just need to tell the all.equal method which two data items we want to compare:

all.equal(Data1, Data2) [1] TRUE

The RStudio console gave a TRUE response, indicating that the vector objects Data1 and Data2 are the same.

Next, let’s use the vector objects Data1 and Data3 to apply the all.equal function:

all.equal(Data1, Data3) [1] "Mean relative difference: 0.01295039"

The average relative difference between our two vector objects, or 0.01295039, was returned by all.equal function.

In other words, Data1 and Data2 are not the same vector objects.

Best Data Science Algorithms » finnstats

### Example 2: The Function all.equal() in Basic Application

The all.equal function’s ability to allow the user to select a tolerance level is a useful feature. The all.equal function does not report differences below this tolerance.

We define a tolerance of 0.1 in the R code below:

all.equal(Data1, Data2, tolerance = 0.1) [1] TRUE

As you can see, even though our two vectors are somewhat different, all.equal method has returned TRUE.