简体   繁体   English

使用na.rm = TRUE时会删除NaN

[英]NaN is removed when 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

To me, NA (missing value) and NaN (not a number) are two completely different entities, why does na.rm remove NaN ? 对我来说, NA (缺失值)和NaN (不是数字)是两个完全不同的实体,为什么na.rm去除NaN How can I ignore NA and not NaN ? 我如何忽略NA而不是NaN

ps:I am using 64-bit R version 3.0.0 on Windows7. ps:我在Windows7上使用的是64位R版本3.0.0。

Edit: Upon some more study I found that is.na returns true for NaN too! 编辑:经过一些研究,我发现is.na也为NaN返回true! This is the cause of confusion for me. 这是我迷茫的原因。

is.na(NaN)
> TRUE

It's a language decision: 这是一个语言决定:

> is.na(NaN)
[1] TRUE

is.nan differentiates: is.nan区分:

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

So you may need to call both. 所以你可能需要同时打电话。

na.rm arguments in functions generally use is.na() or an analogous function. 函数中的na.rm参数通常使用is.na()或类似函数。
And since is.na(NaN) == TRUE , you then get the behavior you're observing. 因为is.na(NaN) == TRUE ,然后你得到你正在观察的行为。

Now should NaN be treated as also NA? 现在应该将 NaN视为NA吗? That is a different question ;) 这是一个不同的问题;)


The best way around this is to explicitly tell R how to handle NaN One example: 解决这个问题的最好方法是明确告诉R如何处理NaN一个例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM