R/check_equality.R

Defines functions check_equality

#' Check whether two vectors are equal
#'
#' Checks whether all values in two vectors are equal.
#' Returns `NA` if vectors contain NA values.
#'
#' @param vector_one The first vector to be compared.
#' @param vector_two The second vector to be compared.
#' @param na.rm Should missing values be removed? Defaults to `FALSE`.
#'
#' @return Returns TRUE, FALSE, or NA.
#' @importFrom stats na.omit
#' @noRd
#'
#' @examples
#' check_equality(vector_one, vector_two)
check_equality <- function(vector_one, vector_two, na.rm = FALSE) {

  # Remove NA values, if na.rm is net to TRUE
  if (na.rm) {
    vector_one <- na.omit(vector_one)
    vector_two <- na.omit(vector_two)
  }

  # Check whether length of the vectors is the same
  if (length(vector_one) != length(vector_two)) {
    return(FALSE)
  }

  # Check whether vectors are equal
  length(vector_one == vector_two) == sum(vector_one == vector_two)
}
JonasEngstrom/prexRiswr documentation built on Dec. 18, 2021, 1:41 a.m.