One function to detect NaN, NA, Inf, -Inf, etc.?
13,599
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
Author by
Suraj
Hi! I'm Suraj and here is a little about me, on my blog That! But How?
Updated on June 06, 2022Comments
-
Suraj almost 2 years
Is there a single function in R that determines if a value is
NA
,NaN
,Inf
,-Inf
, or otherwise not a well-formed number? -
kohske over 12 yearsNote that
is.finite(TRUE)
also returnsTRUE
. -
Joshua Ulrich over 12 years@kohske: Good point.
is.finite(FALSE)
also returnsTRUE
. This is likely becauseTRUE
andFALSE
are just integers. -
hadley over 12 yearsTrue and false aren't integers - but they will be coerced without error/warning message
-
Joshua Ulrich over 12 years@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 returnTRUE
.