Julian way to "remove" missing type in the eltype of a Matrix?

julia> A = [1 2 missing; 4 5 6]
2×3 Matrix{Union{Missing, Int64}}:
 1  2   missing
 4  5  6

julia> A[1,3] = 3
3

julia> A
2×3 Matrix{Union{Missing, Int64}}:
 1  2  3
 4  5  6

Now, I want to get rid of the missing in the eltype and just get Matrix{Int64}.

I can get the nonmissingtype of the eltype and manually convert, or use disallowmissings from DataFrames, but the first is ugly, the second is not in base.

Other ways? Or could simply disallowmissing (and of course allowmissing) be implemented in Base, as they are useful not only for DataFrames…

A=convert(Array{Int}, A)
julia> using Missings

julia> disallowmissing(A)
2×3 Matrix{Int64}:
 1  2  3
 4  5  6
2 Likes

yes, but you first need to know that the eltype is indeed Int.

In general you can do convert(Array{nonmissingtype(eltype(A)),ndims(A)}, A) but it’s ugly…

Yep, I missed the point that you do not know the nonmissingtype of the eltype

Thanks, I didn’t think about Missings.jl… still would be useful having it in Base

julia> A
2×3 Matrix{Union{Missing, Int64}}:
 1  2  3
 4  5  6

julia> identity.(A) # or map(identity, A)
2×3 Matrix{Int64}:
 1  2  3
 4  5  6

yes - you can also do identity.(A). The difference is that disallowmissing(A) is a no-op, if A already does not allow missing values, while map or broadcast always allocates a new object.

1 Like