Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is.na status of NaN values in R

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading