R Special Numbers

R supports four numeric special values that help in arithmatic. Those are -

  • Inf
  • -Inf
  • NaN
  • NA

The first two are positive and negative infinity. The NaN stands for 'Not a number' means that our calculation either didn't make mathematical sense or could not be performed properly and NA stands for 'Not available' that represents a missing value.

Example

> c(Inf + 1, Inf - 1, Inf - Inf)
[1] Inf Inf NaN
> c(1 / Inf, Inf / 1, Inf / Inf)
[1]   0 Inf NaN
> c(NA + 1, NA * 5, NA + Inf)
[1] NA NA NA
> c(NA + NA, NaN + NaN, NaN + NA, NA + NaN)
[1]  NA NaN NaN  NA

We can also check the value is a special values or not, like -

> x <- c(0, NA, -Inf, NaN, Inf)
> is.finite(x)
[1]  TRUE FALSE FALSE FALSE FALSE
> is.infinite(x)
[1] FALSE FALSE  TRUE FALSE  TRUE
> is.nan(x)
[1] FALSE FALSE FALSE  TRUE FALSE
> is.na(x)
[1] FALSE  TRUE FALSE  TRUE FALSE