Julia Missing Values
Missing values stand for data that is not known. A missing value is not zero, and it is not an error.
A test tells whether a value is missing
The second value is missing. The first value is known.
values = [10, missing, 20]
println(ismissing(values[1]))
println(ismissing(values[2]))Skipped values leave only known numbers
Missing values are dropped before the total. Only known values are added.
values = [5, missing, 7]
println(sum(skipmissing(values)))
println(length(collect(skipmissing(values))))A mean uses only known values
The known values are added, then divided by their count. The missing value is not counted.
values = [10, missing, 20]
known = collect(skipmissing(values))
println(sum(known))
println(sum(known) / length(known))Example
scores = [80, missing, 90, missing, 70]
known = collect(skipmissing(scores))
println(length(known))
println(sum(known))
println(sum(known) / length(known))Key points
- Missing: a value that means no value was recorded.
- Use ismissing to test whether a value is missing.
- Use skipmissing to drop missing values before a calculation.
- Use sum with skipmissing to total only the known values.
- Use length with skipmissing to count only the known values.
In this example, missing scores are dropped, the known scores are counted and added, and the mean is the known total divided by the known count.
Note
Arithmetic with a missing value gives a missing value.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
Scores are 80, missing, and 90. What is the mean of the known scores?