Lastly, the libr package contains an enhanced
equality operator. The objective of the %eq%
operator is to
return a TRUE or FALSE value when any two objects are compared. This
enhanced equality operator is useful for situations when you don’t want
to check for NULL or NA values, or care about the data types of the
objects you are comparing.
The %eq%
operator also compares data frames. The
comparison will include all data values, but no attributes. This
functionality is particularly useful when comparing tibbles, as tibbles
often have many attributes assigned by dplyr
functions.
While it can be advantageous to have a comparison operator that does
not give errors when encountering a NULL or NA value, note that this
behavior can also mask problems with your code. Therefore, use the
%eq%
operator with care.
Below is an example of several comparisons using the
%eq%
infix operator:
library(libr)
# Comparing of NULLs and NA
NULL %eq% NULL # TRUE
NULL %eq% NA # FALSE
NA %eq% NA # TRUE
1 %eq% NULL # FALSE
1 %eq% NA # FALSE
# Comparing of atomic values
1 %eq% 1 # TRUE
"one" %eq% "one" # TRUE
1 %eq% "one" # FALSE
1 %eq% Sys.Date() # FALSE
# Comparing of vectors
<- c("A", "B", "C")
v1 <- c("A", "B", "C", "D")
v2 %eq% v1 # TRUE
v1 %eq% v2 # FALSE
v1
# Comparing of data frames
%eq% mtcars # TRUE
mtcars %eq% iris # FALSE
mtcars %eq% iris[1:50,] # FALSE
iris
# Mixing it up
%eq% NULL # FALSE
mtcars %eq% NA # FALSE
v1 1 %eq% v1 # FALSE
Next: Disclaimer