squaregoldfish
squaregoldfish

Reputation: 820

Julia: Add missing value to array

I have an array that can take Float64 and Missing values:

local x::Array{Union{Float64, Missing}, 1} = [1.0, missing, 3.0]

I can add more Float64 values using the append! function, but I can't add a missing value this way. I get the following error:

julia> append!(x, missing)
ERROR: MethodError: no method matching length(::Missing)

What's the correct way to add missing values to this array?

Upvotes: 1

Views: 685

Answers (2)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42264

Yes you are right that push! should be used. Additionally your code does not need to be so verbose:

julia> x = [1.0, missing, 3.0]
3-element Array{Union{Missing, Float64},1}:
 1.0
  missing
 3.0

julia> y = Union{Missing, Float64}[]
0-element Array{Union{Missing, Float64},1}

julia> push!(y,1);

julia> push!(y,missing)
2-element Array{Union{Missing, Float64},1}:
 1.0
  missing

Moreover, instead of Array{Union{Float64, Missing}, 1} the shorter and more readable version Vector{Union{Float64, Missing}} can be used.

Upvotes: 5

squaregoldfish
squaregoldfish

Reputation: 820

I should have been using push! - append! is for adding collections, while push! is for single values.

Upvotes: 1

Related Questions