NaN is removed by using na.rm = TRUE

This reproducible example is a very simplified version of my code:

x <- c(NaN, 2, 3) #This is fine, as expected max(x) > NaN #Why does na.rm remove NaN? max(x, na.rm=TRUE) > 3 

For me, NA (missing value) and NaN (rather than a number) are two completely different objects, why does na.rm remove NaN ? How can I ignore NA , not NaN ?

ps: I am using the 64-bit version of R.0.0.0.0 on Windows7.

Edit: In another study, I found that is.na also returns true for NaN ! This is the reason for the confusion for me.

 is.na(NaN) > TRUE 
+8
r nan na
source share
2 answers

This solution is in language:

 > is.na(NaN) [1] TRUE 

is.nan distinguishes between:

 > is.nan(NaN) [1] TRUE > is.nan(NA) [1] FALSE 

Thus, you may need to call both.

+6
source share

na.rm arguments in functions usually use is.na() or a similar function.
And since is.na(NaN) == TRUE , you get the behavior you observe.

Now should NaN be considered as NA? This is another question;)


The best thing is to explicitly tell R how to handle NaN One example:

 ifelse(any(is.nan(x)), NaN, min(x, na.rm=TRUE)) 
+3
source share

All Articles