An NAN value in R represents “NOT A NUMBER”, It is basically any numeric calculation with an undefined result, such as ‘0/0’. This exists only in vectors with numeric datatype. Even one NAN value in a vector causes difficulty to carry out calculations. Hence, we need to remove or replace it before carrying out any calculation.
Before we actually remove or replace them, we need to check whether there is an NAN number in a vector and for this we use is.nan() function.
This recipe demonstrates how to find if a vector has NAN values.
We create two vectors, one with NAN values and the other without it.
a = c(2,5,8,20,NaN, 35,NaN) b = c(2,5,8,20,75,35,100)
we use any() and is.nan() function to check for NaN values. It returns a logical value TRUE even if 1 Nan value is present.
#checking for NaN values in "a" vector any(is.nan(a))
#checking for NaN values in "b" vector any(is.nan(b))