# How to handle missing values in conditions

**URL:** <https://discourse.julialang.org/t/how-to-handle-missing-values-in-conditions/53736>\
**Category:** New to Julia\
**Created:** [January 21, 2021, 6:02pm UTC](https://discourse.julialang.org/t/how-to-handle-missing-values-in-conditions/53736 "2021-01-21T18:02:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![max\_schwarzer](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@max\_schwarzer](https://discourse.julialang.org/u/max_schwarzer)\
**Post date:** [January 21, 2021, 6:02pm UTC](https://discourse.julialang.org/t/how-to-handle-missing-values-in-conditions/53736/1 "2021-01-21T18:02:26Z")

</div>

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.

```julia
# 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]

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 21, 2021, 6:22pm UTC](https://discourse.julialang.org/t/how-to-handle-missing-values-in-conditions/53736/2 "2021-01-21T18:22:42Z")

</div>

[https://dataframes.juliadata.org/stable/man/missing/](https://dataframes.juliadata.org/stable/man/missing/)

specifically, `coalesce`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 21, 2021, 6:29pm UTC](https://discourse.julialang.org/t/how-to-handle-missing-values-in-conditions/53736/3 "2021-01-21T18:29:37Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![max\_schwarzer](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@max\_schwarzer](https://discourse.julialang.org/u/max_schwarzer)\
**Post date:** [January 21, 2021, 6:32pm UTC](https://discourse.julialang.org/t/how-to-handle-missing-values-in-conditions/53736/4 "2021-01-21T18:32:40Z")

</div>

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