NaN is removed when using na.rm=TRUE

守給你的承諾、 提交于 2019-11-28 00:05:12

问题


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

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

ps:I am using 64-bit R version 3.0.0 on Windows7.

Edit: Upon some more study I found that is.na returns true for NaN too! This is the cause of confusion for me.

is.na(NaN)
> TRUE

回答1:


It's a language decision:

> is.na(NaN)
[1] TRUE

is.nan differentiates:

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

So you may need to call both.




回答2:


na.rm arguments in functions generally use is.na() or an analogous function.
And since is.na(NaN) == TRUE, you then get the behavior you're observing.

Now should NaN be treated as also NA? That is a different question ;)


The best way around this is to explicitly tell R how to handle NaN One example:

ifelse(any(is.nan(x)), NaN, min(x, na.rm=TRUE))


来源:https://stackoverflow.com/questions/16028446/nan-is-removed-when-using-na-rm-true

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!