# Debugging a script to replace NaN or -999 values with missing values

**URL:** <https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 22, 2021, 1:38pm UTC](https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214 "2021-10-22T13:38:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 22, 2021, 1:38pm UTC](https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214/1 "2021-10-22T13:38:10Z")

</div>

I’m trying to write a program to replace all NaN values or -999 values in my matrix B with missing values (write down nothing) before I use Dataframe to put them into CSV files).

Below is my code:

```julia
B = [-0 0 1; NaN 0.2 7; 0 NaN -999; NaN 15 NaN];
replace!(x -> (isnan.(x) || x .== -999) ? missing : x, B)

```

Here is the error message:  
`MethodError: Cannot `convert` an object of type Missing to an object of type Float64`

Why doesn’t it work? Thanks!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 22, 2021, 1:42pm UTC](https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214/2 "2021-10-22T13:42:44Z")

</div>

`B` is a `Vector{Float64}`. Therefore, it can only store `Float64` values. If you define `B` as `B = Union{Float64, Missing}[-0 0 1; NaN 0.2 7; 0 NaN -999; NaN 15 NaN];`, it would work.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 22, 2021, 1:48pm UTC](https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214/3 "2021-10-22T13:48:41Z")

</div>

Thanks for the reply!

This is a simplified example. In reality, my B matrix is huge and it is read out of another data source. In that case, how do I make the conversion? I tried the below but it did not work:  
`B = Union{Float64, Missing}(B)`

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [October 22, 2021, 1:54pm UTC](https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214/4 "2021-10-22T13:54:00Z")

</div>

You want to use `B = Matrix{Union{Float64,Missing}}(B)`.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 22, 2021, 1:54pm UTC](https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214/5 "2021-10-22T13:54:58Z")

</div>

This will make a copy, but that is unavoidable since `Matrix{Union{Float64,Missing}}` has a different memory layout than `Matrix{Float64}`

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [October 22, 2021, 1:57pm UTC](https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214/6 "2021-10-22T13:57:25Z")

</div>

agreed, but @leon could allocate `B` first as a `Matrix{Union{Float64,Missing}}` and then read the data from source into this allocation?

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 22, 2021, 2:15pm UTC](https://discourse.julialang.org/t/debugging-a-script-to-replace-nan-or-999-values-with-missing-values/70214/7 "2021-10-22T14:15:17Z")

</div>

Many thanks, All. This addressed my issue nicely.
