R:为什么是 mean(NA, na.rm = TRUE) return NaN

R: Why does mean(NA, na.rm = TRUE) return NaN

当使用所有 NA 的向量估计平均值时,如果 na.rm = TRUE,我们得到 NaN。这是为什么,这是有缺陷的逻辑还是我遗漏了什么?使用 NA 肯定比 NaN?

更有意义

下面的快速示例

mean(NA, na.rm = TRUE)
#[1] NaN

mean(rep(NA, 10), na.rm = TRUE)
#[1] NaN

来自 mean 文档:

na.rm a logical value indicating whether NA values should be stripped before the computation proceeds.

使用此逻辑,在应用函数均值之前删除所有 NA。在您的情况下,您对任何事情都没有任何意义(所有 NA 都被删除),因此返回 NaN。

有点可惜?mean对此没有说什么。 only told you that applying mean on an empty "numeric" results in NaN without more reasoning. tried to reason this but was not accurate, as division by 0 is not always NaN, it can be Inf or -Inf. I once discussed about this in 。然而,我们越来越接近了。虽然mean(x)没有被sum(x) / length(x)编码,但是这个数学事实确实解释了这个NaN.

来自?sum:

 *NB:* the sum of an empty set is zero, by definition.

所以 sum(numeric(0))0。由于 length(numeric(0))0mean(numeric(0))0 / 0NaN.