#' ISWR exercise 1.1
#'
#' @description Solution to exercise 1.1 from Basics in Dalgaard P. Introductory
#' statistics with R. New York : Springer; 2008.
#'
#' @description “How would you check whether two vectors are the same if they may
#' contain missing (`NA`) values? (Use of the `identical` function is considered
#' cheating!)”
#'
#' @param vector_one The first vector to be checked.
#' @param vector_two The second vector to be checked.
#'
#' @return Returns `TRUE` or `FALSE`.
#' @export
#'
#' @examples
#' exercise_1_1(c(1, 2, NA, 4), c(1, NA, 3, 4))
exercise_1_1 <- function(vector_one, vector_two) {
# Get indices of NA values in both vectors
vector_one_indices <- na_indices(vector_one)
vector_two_indices <- na_indices(vector_two)
# Check whether number of NA values and their indices are the same
if (!check_equality(vector_one_indices, vector_two_indices)) {
return(FALSE)
# Check whether non-NA values in vectors are the same
} else if (!check_equality(vector_one, vector_two, na.rm = TRUE)) {
return(FALSE)
} else {
return(TRUE)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.