R Tutorial R Charts & Graphs R Statistics R References

R - is.nan() Function



The R is.nan() function is used to check if a numeric value is NaN and returns a boolean result. A complex number is regarded as NaN if either the real or imaginary part is NaN.

Syntax

is.nan(x)

Parameters

x Required. Specify column to compute on.

Return Value

Returns TRUE if the value is NAN, FALSE otherwise.

Example:

The example below shows the usage of is.nan() function.

#operating on single element atomic vector
print(is.nan(0.0/0.0))
print(is.nan(10.5))
print(is.nan(1.0/0.0))
print(is.nan(1+2i))

cat("\nOperating on vector\n")
#operating on vector
v <- c(NaN, 105, 20.8, NaN+2i)
print(is.nan(v))

cat("\nOperating on matrix\n")
#operating on matrix
m <- matrix(c(1, 2, 3, 4, Inf, NaN), nrow=2)
print(is.nan(m))

cat("\nOperating on first column of matrix\n")
#operating on first column of matrix
print(is.nan(m[,1]))

The output of the above code will be:

[1] TRUE
[1] FALSE
[1] FALSE
[1] FALSE

Operating on vector
[1]  TRUE FALSE FALSE  TRUE

Operating on matrix
      [,1]  [,2]  [,3]
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE  TRUE

Operating on first column of matrix
[1] FALSE FALSE

❮ R Math Functions

5