How to handle missing in boolean context in Julia?

Use the coalesce function like this:

julia> x = [1,2,3,missing,5,6,7]
7-element Array{Union{Missing, Int64},1}:
 1
 2
 3
  missing
 5
 6
 7

julia> @. ifelse(coalesce(x < 4.7, false), "small", missing)
7-element Array{Union{Missing, String},1}:
 "small"
 "small"
 "small"
 missing
 missing
 missing
 missing

As a side note do not write df[:size] (this syntax has been deprecated for over 2 years now and soon it will error) but rather df.size or df."size" to access the column of the data frame (the df."size" is for cases when your column names contain characters like spaces etc., e.g. df.”my fancy column!”).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top