So I’m developing this function
#' Disambiguated Equality operator between two vectors that may contain NA's
#'
#' Forces the equality result to be TRUE if both corresponding vector elements are either equal, or both NA.
#' @param x vector of any type compatible with is.na and ==.
#' @param y vector of any type compatible with is.na and ==.
#' @export
#' @examples
#' c(NA, 1:5) %==% c(NA, 1:3,"nope", NA) #[1] TRUE TRUE TRUE TRUE FALSE FALSE
gp.is.equal.force <- `%==%` <- function(x, y, vect = T) {
res <- (is.na(x) & is.na(y)) | (!is.na(x) & !is.na(y) & x == y)
if (!vect) res <- all(res)
res
}
I noticed a behaviour that I wouldn’t have expected regarding NaN values on R version 4.3.2 (2023-10-31) — "Eye Holes".
> is.na(NaN) # [1] TRUE
> is.na(c(NaN, 5)) # [1] TRUE FALSE
> is.na(c(NaN, NA)) # [1] TRUE TRUE
> is.nan(c(NaN, NA)) # [1] TRUE FALSE
> is.na(c(NaN, as.factor("abc"))) # [1] TRUE FALSE
> is.nan(c(NaN, 5)) # [1] TRUE FALSE
so far it’s all good, however:
> is.na(c(NA, NaN, "abc")) # [1] TRUE FALSE FALSE
> is.nan(c(NA, NaN, "abc")) # [1] FALSE FALSE FALSE
> is.nan(c(NaN, "abc")) #[1] FALSE FALSE
Seems like adding characters into the mix breaks the logic somehow for NaN values.
Isn’t it peculiar?
>Solution :
From ?NaN (my bold),
Inf and -Inf are positive and negative infinity whereas NaN means ‘Not a Number’. (These apply to numeric values and real and imaginary parts of complex values but not to values of integer vectors.)
Adding characters into a vector will coerce that vector into character type, therefore the NaN no longer represents "not a number", but the character "NaN".
c(NA, NaN, "abc")
[1] NA "NaN" "abc"
As expected, is.nan will return FALSE for characters.
is.nan(c(NaN, "NaN"))
[1] FALSE FALSE
is.nan(c(NaN, NaN))
[1] TRUE TRUE