49

Is there a single function in R that determines if a value is NA, NaN, Inf, -Inf, or otherwise not a well-formed number?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Suraj
  • 35,905
  • 47
  • 139
  • 250

1 Answers1

66

You want is.finite

> is.finite(NA)
[1] FALSE
> is.finite(NaN)
[1] FALSE
> is.finite(Inf)
[1] FALSE
> is.finite(1L)
[1] TRUE
> is.finite(1.0)
[1] TRUE
> is.finite("A")
[1] FALSE
> is.finite(pi)
[1] TRUE
> is.finite(1+0i)
[1] TRUE
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 1
    Note that `is.finite(TRUE)` also returns `TRUE`. – kohske Sep 23 '11 at 01:00
  • 1
    @kohske: Good point. `is.finite(FALSE)` also returns `TRUE`. This is likely because `TRUE` and `FALSE` are just integers. – Joshua Ulrich Sep 23 '11 at 01:09
  • 9
    True and false aren't integers - but they will be coerced without error/warning message – hadley Sep 25 '11 at 14:50
  • 5
    @hadley: I wasn't clear; thanks for clarifying. I was referring to R booleans being 32-bit integers at the C level, not that `is.integer(TRUE)` would return `TRUE`. – Joshua Ulrich Sep 25 '11 at 15:10