Reputation: 403
I'm using julia-0.7. The following code
using Statistics
x=[missing, 0]
mean(skipmissing(x))
gives 0 where missing
is the built-in missing value of julia-0.7. If I further try
x[2]=missing
mean(skipmissing(x))
I get error message. What is the best way to get missing
instead of error in the second case?
I'm afraid of using if
/else
because this snippet is iterated many times in the program.
Upvotes: 0
Views: 42
Reputation: 69819
The fastest code I could recommend is using if/else
, but I guess you cannot avoid it:
sx = skipmissing(x)
iterate(sx) === nothing ? missing : mean(sx)
as iterate
should be faster than e.g. length
.
However, in general you point to a problem that has arisen to me also recently, as there is an inconsistency in design of mean
, because if you collect
the you get:
julia> mean(collect(skipmissing(x)))
NaN
in case you have presented (so not an error nor missing
).
Upvotes: 1