Skip to contents

The following functions have in common that they return a vector of logical.

Function allAreEqual()

This function checks if all values in a vector are equal to each other:

kwb.utils::allAreEqual(c(1, 1, 1, 1))
#> [1] TRUE
kwb.utils::allAreEqual(c(1, 1, 1, 2))
#> [1] FALSE

Function allAreIdentical()

This function checks if all elements in a list are identical to each other. It may be useful when checking whether different versions of a function (that may, e.g., be created during code cleaning) return exactly the same, when being given the same inputs.

# Define different functions that are intended to do the same
get_list_1 <- function(a, b) list(a = a, b = b)
get_list_2 <- function(a, b) stats::setNames(list(a, b), c("a", "b"))
get_list_3 <- function(a, b) c(list(a = a), b = b)
get_list_4 <- function(a, b) c(list(a = a), list(b = b))

# Call the functions with identical arguments and put the results into a list
results <- list(
  get_list_1(1, 2:3), 
  get_list_2(1, 2:3), 
  get_list_3(1, 2:3),
  get_list_4(1, 2:3)
)

# Not all results are the same...
kwb.utils::allAreIdentical(results)
#> [1] FALSE

# ... but all except the third:
kwb.utils::allAreIdentical(results[-3])
#> [1] TRUE

Function almostEqual()

Take care when comparing floating point numbers! Whether floating point numbers are assumed to be equal or not, depends on how they were calculated. This is shown in the following example:

one_third_1 <- 1/3
one_third_2 <- 1 - 2/3

# Even though mathematically correct, they are not equal in R:
one_third_1 == one_third_2
#> [1] FALSE

With almostEqual() numbers are compared by tolerating a small difference between the numbers. This difference can be set with the argument tolerance.

# The numbers are almost equal (with the default tolerance of 1e-12):
kwb.utils::almostEqual(one_third_1, one_third_2)
#> [1] TRUE

# It depends on the tolerance if they are treated as equal:
kwb.utils::almostEqual(one_third_1, one_third_2, tolerance = 1e-17)
#> [1] FALSE

Function atLeastOneRowIn()

Function containsNulString()

Function hsValidValue()

Function inRange()

Function is.unnamed()

Function isEvenNumber()

Function isLoaded()

Function isNaInAllColumns()

Function isNaInAllRows()

Function isNaOrEmpty()

Function isNullOrEmpty()

Function isOddNumber()

Function matchesCriteria()