Search code examples
arraysjuliamissing-data

Convert missing Array to NaN


Lets take this example where I use the ShiftedArrays package lag() function. I compare array o with array c. If o is less than lag(c,1) say true, false.

# dummy data
o = collect(5.0:1.0:14)
c = collect(1.0:1.0:10)
# lag
using ShiftedArrays
o_c_lag1 = o .< lag(c,1)

With output:

print(o_c_lag1)
Any[missing, false, false, false, false, false, false, false, false, false]

My question here. Is there a succient way to make my o and c bool comparison whilst converting the missing to a NaN and output type of Float64?

The type I have on my output is Any. I am looking to write fast code. So I want it to be Float64.

If I make a NaN:

nan = [NaN]
1-element Array{Float64,1}:
 NaN

I will be writing thousands of variations of this line of code:

o_c_lag1 = o .< lag(c,1)

Is there a way I can convert the Missing type in one line to NaN? So I can output Float64?

It would be cumbersome to change thousands of Arrays.

** Edit

Maybe I can store all Arrays inside R equivalent list(). And iterate over all Arrays, changing all missing to NaN and convert to type Float64.


Solution

  • I'm not familiar with the ShiftedArrays package but you can do the following:

    julia> o_c_lag1
    10-element Array{Any,1}:
          missing
     false
     false
     false
     false
     false
     false
     false
     false
     false
    
    julia> Float64.(replace(o_c_lag1, missing => NaN)))
    10-element Array{Float64,1}:
     NaN
       0.0
       0.0
       0.0
       0.0
       0.0
       0.0
       0.0
       0.0
       0.0
    

    You can use replace(x, missing => NaN) to change missing to NaN and then simply convert the array to Float64s.