dplyr near(): compare if numbers and vectors are nearly the same

In this tutorial, we will learn near() function part of R package dplyr to check the equality of two numbers or vectors. Often we would like to compare if two numbers or two vectors are equal. A naive approach of using == to compare if two numbers are equal can be problematic due to machine precision issues.

near() function to test if two numbers are equal

For example, if you try the simple expression in R, you will get a surprising result.

sqrt(10) ^ 2 == 10

FALSE

Here is another example where will see surprising result

0.1 + 0.1 + 0.1 == 0.3

FALSE

A better solution is to use near() function available with dplyr(). near() function makes it really easy to compare two floating numbers or vectors. With near() function we can make pairwise comparison and check if they are equal.near() is much safer than using “==”, as it provides a tolerance level argument to check if two numbers are equal.

library(dplyr)

Now with near(), we get the expected answer when we check equality of two numbers.

near(sqrt(10) ^ 2,  10)

TRUE

near() example to check if two vectors are equal

By default, near() function uses “.Machine$double.eps^0.5”. For example, if we check the two following vectors

near(c(1,2,3),  c(1.00001,2,3))

We would get

FALSE TRUE TRUE

By specifying tolerance level

near(c(1,2,3),  c(1.00001,2,3), tol = 0.001)

We would get that the elements of the vector are all nearly equal.

 TRUE TRUE TRUE
Exit mobile version