I found this nice function called coalesce and now I am wondering if there is a more general one. Is there a function like coalesce which can specify the missing value/type.
a = [NaN,missing,"",2,3.0]
coalesce.(a,0)
coalesce.(a,0,missing="") # fails
coalesce.(a,0,missing=NaN) # fails
I think replace(a, NaN => 0) is what you are looking for. For multiple replacements: replace(a, NaN => 0, missing => 0, "" => 0).
Unfortunately, the result still has type Vector{Any} instead of Vector{Float64}.
Indeed, replace allocates the final vector before going through the elements. So the “type narrowing” is done only using the initial type of the vector and the replacement pairs (e.g. for eltype(a) == Union{Missing, Float64} and pair missing => 0.0 , it’s possible to determine before any pass on the source vector that the destination vector will have eltype Float64 ).
Unfortunately ? : is a bit different — it’s parsed directly to an if statement.
julia> Meta.parse("a ? b : c")
:(if a
b
else
c
end)
So that’s why we didn’t deprecate its use within @. expressions like we did || and && long ago (which is what made space for the above). Making arbitrary if clauses participate in broadcast is a much bigger thing.