It is a bit pity that ?mean
does not say anything about this. My comment only told you that applying mean
on an empty "numeric" results in NaN
without more reasoning. Rui Barradas's comment 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 R: element-wise matrix division. However, we are getting close. Although mean(x)
is not coded by sum(x) / length(x)
, this mathematical fact really explains this NaN
.
From ?sum:
*NB:* the sum of an empty set is zero, by definition.
So sum(numeric(0))
is 0
. As length(numeric(0))
is 0
, mean(numeric(0))
is 0 / 0
which is NaN
.
NaN
. As for whether it makes more sense, I believe it doesn't, since you have removed the missing values. – Teacartmean(numeric(0))
– Jaborandimean(as.Date(NA), na.rm = TRUE)
is NA and not NaN though – Tiber