0
votes

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.

1
I have given an answer what I think is best for now, but in general it seems broken. I have opened an issue github.com/JuliaLang/julia/issues/28777. - Bogumił Kamiński

1 Answers

1
votes

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).