I would like to create a function that deals with missing values. However, when I tried to specify the missing type Array{Missing, 1}, it errors.
function f(x::Array{<:Number, 1})
# do something complicated
println("no missings.")
println(sum(x))
end
function f(x::Array{Missing, 1})
x = collect(skipmissing(x))
# do something complicated
println("removed missings.")
f(x)
end
f([2, 3, 5])
f([2, 3, 5, missing])
I understand that my type is not Missing but Array{Union{Missing, Int64},1}
When I specify this type, it works in the case above. However, I would like to work with all types (strings, floats etc., not only Int64
).
I tried
function f(x::Array{Missing, 1})
...
end
But it errors again... Saying that
f (generic function with 1 method)
ERROR: LoadError: MethodError: no method matching f(::Array{Union{Missing, Int64},1})
Closest candidates are:
f(::Array{Any,1}) at ...
How can I say that I wand the type to be union missings with whatever?
EDIT (reformulation)
Let's have these 4 vectors and two functions dealing with strings and numbers.
x1 = [1, 2, 3]
x2 = [1, 2, 3, missing]
x3 = ["1", "2", "3"]
x4 = ["1", "2", "3", missing]
function f(x::Array{<:Number,1})
println(sum(x))
end
function f(x::Array{String,1})
println(join(x))
end
f(x)
doesn't work for x2 and x3, because they are of type Array{Union{Missing, Int64},1}
and Array{Union{Missing, String},1}
, respectively.
It is possible to have only one function that detects whether the vector contains missings, removes them and then deals appropriately with it.
for instance:
function f(x::Array{Any, 1})
x = collect(skipmissing(x))
print("removed missings")
f(x)
end
But this doesn't work because Any
indicates a mixed type (e.g., strings and nums) and does not mean string OR numbers or whatever.
EDIT 2 Partial fix
This works:
function f(x::Array)
x = collect(skipmissing(x))
print("removed missings")
f(x)
end
[But how, then, to specify the shape (number of dimensions) of the array...? (this might be an unrelated topic though)]
You can do it in the following way:
and now it works:
EDIT:
I will try to answer the questions raised (if I miss something please add a comment).
First
Vector{Union{Missing, <:Number}}
is the same asVector{Union{Missing, Number}}
because of the scoping rules as tibL indicated asVector{Union{Missing, <:Number}}
translates toArray{Union{Missing, T} where T<:Number,1}
andwhere
clause is insideArray
.Second (here I am not sure if this is what you want). I understand you want the following behavior:
Note the last two line - although
["a", 'a']
does not contain missing the array hasAny
element type so it might contain missing. The last case excludes it.Also you can see that you could change the second parameter of
Array{T,N}
to something else to get a different dimensionality.Also this example works because the first method, as more specific, catches all cases that allow
Missing
and a second method, as more general, catches what is left (i.e. essentially what does not allowMissing
).