Section 31 Missing values

31.1 Missing values in R

  • NA

  • NaN

  • is.na() to test objects if they are NA

  • is.nan() to test object for NaN

  • NA values have a class

    • integer NA, character NA, logical NA
  • A NaN value is also NA but the converse is not true

  • A special number: Inf

  • Inf and -Inf are positive and negative infinity whereas NaN means ‘Not a Number’.

31.2 Example: Numeric vector

x <- c(1, 2, NA, 10, 3)
is.na(x)
is.nan(x)

y <- c(1, 2, NaN, NA, 4)
is.na(y)
is.nan(y)


sqrt(-2)
is.nan(sqrt(-2))

5/0

31.3 Example: Character vector

x <- c('a', 'b', NA, 'd', 'e')
is.na(x)
is.nan(x)

y <- c('a', 'b', NaN, NA, 'e')
is.na(y)
is.nan(y)

31.4 Example: Logical vector

x <- c(T, F, NA, TRUE, FALSE)
is.na(x)
is.nan(x)

y <- c(T, F, NaN, NA, TRUE, FALSE)
is.na(y)
is.nan(y)