How to Use the near() Function in dplyr


Often you may want to check if two vectors of floating point numbers are pairwise equal in R.

Fortunately this is easy to do by using the near() function from the dplyr package in R, which is designed to perform this exact task.

The near() function uses the following basic syntax:

near(x, y, tol)

where:

  • x: The first numeric vector to compare
  • y: The second numeric vector to compare
  • tol: The tolerance of comparison

Note that this function is particularly useful because you can specify a tolerance level if you would like to consider two values to be equal if their difference is less than some value.

The following example shows how to use the near() function from the dplyr package to compare two vectors in practice.

Note: Before using the near() function, you may need to first install the dplyr package by using the following syntax:

install.packages('dplyr')

Once the dplyr package is installed, you can use the near() function.

Example: How to Use the near() Function in dplyr

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)

Suppose that we would like to check if the two vectors are pairwise equal.

We can use the near() function from the dplyr package to do so:

library(dplyr)

#check if the two vectors are pairwise equal
near(vector1, vector2)

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

This returns a vector of Boolean values that tell us whether each pairwise set of numbers is equal between the two vectors.

Since this returns TRUE for each element in the resulting vector, it tells us that every single pairwise set of numbers is equal between the vectors.

Note that we can also use the following trick to check if all values in the resulting vector are equal to TRUE:

#check if all values are equal between two vectors
sum(near(vector1, vector2)) == length(vector1)

[1] TRUE

This returns a single value of TRUE, which tells us that all pairwise values between the two vectors are equal.

This syntax works by using the fact that TRUE evaluates to 1 when summed, so we effectively check if the sum of all elements resulting from the near() function are equal to the total number of elements in one of the vectors (in this case we chose to use vector1).

If this instead returned a value of FALSE then we would know that at least one pairwise set of numbers is not equal between the two vectors.

Now suppose we change the last value in vector2 to a different number and then use the near() function again to compare the vectors:

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

#check if all values are equal between two vectors
sum(near(vector1, vector2)) == length(vector1)

[1] FALSE

This returns FALSE, which tells us that at least one pairwise set of numbers is not equal between the two vectors.

Note: You can find the complete documentation for the near() function from the dplyr package here.

Additional Resources

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

How to Use slice_min() in dplyr
How to Use the pull() Function in dplyr
How to Use top_n() in dplyr
How to Rename Columns Using dplyr

Leave a Reply

Your email address will not be published. Required fields are marked *