Julia Cleaning Data
Cleaning data replaces messy values with clean values that code can use. A clean value has a fixed form, so later steps need no special cases.
A default replaces a missing value
The first value is missing, so it becomes zero. The second value is known, so it stays five.
println(coalesce(missing, 0))
println(coalesce(5, 0))A failed parse falls back to a fixed default
Well formed text parses to a number. Bad text falls back to zero.
function parse_or_default(s, default)
try
return parse(Int, s)
catch
return default
end
end
println(parse_or_default("42", 0))
println(parse_or_default("bad", 0))A cleaned list has no missing values left
Each missing value becomes zero. The cleaned list can then be added.
values = [missing, 10, missing]
cleaned = [coalesce(v, 0) for v in values]
println(cleaned)
println(sum(cleaned))Example
function parse_or_default(s, default)
try
return parse(Int, s)
catch
return default
end
end
raw = ["90", "bad", "70"]
cleaned = [parse_or_default(s, 0) for s in raw]
missing_score = missing
println(cleaned)
println(coalesce(missing_score, 0))
println(sum(cleaned))Key points
- Use coalesce to replace a missing value with a fixed default.
- Use parse to turn well formed text into a number.
- Use try to attempt a parse that might fail.
- Use catch to supply a fixed default when a parse fails.
- Use a fixed default to keep every cleaned value in the same form.
In this example, each text score is parsed or replaced with zero, a missing score is replaced with zero, and the cleaned scores add to 160.
Note
A default of zero keeps a total numeric, but it is a stand-in, not a measured value.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
What does coalesce(missing, 7) return?