How to handle missing values in conditions

I have a dataframe with zeros and missing values and I would like to create an indicator array that assigns a one if the value is zero and a zero otherwise. I would like missing values to be included in this otherwise statement, meaning they should be assigned a zero too as they are not zero. I am unsure how to achieve this.

# Input
df = DataFrame(a = [0.0, missing, 3.0], b = [4.0, 5.0, 0.0])

# What I'm doing
not_zeros = .!iszero.(Matrix(df))
zeros = iszero.(Matrix(df))

# Desired Output 
not_zeros2 = [0, 1, 1 ; 1, 1, 0]
zeros2 = [1, 0, 0 ; 0, 0, 1]

https://dataframes.juliadata.org/stable/man/missing/

specifically, coalesce

1 Like

coalesce is a good general solution, but in this specific instance you can just use isequal, which returns false for missing:

julia> df = DataFrame(a = [0.0, missing, 3.0], b = [4.0, 5.0, 0.0])
3×2 DataFrame
 Row │ a          b       
     │ Float64?   Float64 
─────┼────────────────────
   1 │       0.0      4.0
   2 │ missing        5.0
   3 │       3.0      0.0

julia> isequal.(0.0, df)
3×2 DataFrame
 Row │ a      b     
     │ Bool   Bool  
─────┼──────────────
   1 │  true  false
   2 │ false  false
   3 │ false   true
1 Like

Thanks, both solutions work great, I went with isequal as it is a bit more elegant