# How do I obtain the \`T\` values in a vector \`Vector{Union{Missing, T}}\`?

**URL:** <https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455>\
**Category:** New to Julia\
**Created:** [September 6, 2019, 3:40am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455 "2019-09-06T03:40:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 6, 2019, 3:40am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/1 "2019-09-06T03:40:07Z")

</div>

Say I have a vector of billion length long and I want to retrieve the `T`-types values, and where it is missing, I want to replace it with ANY value. E.g.

```julia
x = [1, missing, 2]

getT(x)
# shoud return
# [1, T, 2]
# where T can be any value such tthat `T <: Int64`

```

What’s a fast way to do that? I think `Missings.replace |> collect` will be slow due to the need to replace.

```julia

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 6, 2019, 4:17am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/2 "2019-09-06T04:17:16Z")

</div>

`coalesce` maybe?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 6, 2019, 4:30am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/3 "2019-09-06T04:30:53Z")

</div>

I think the implementation will do some ifs. BEst to just extract the underlying array of bytes… Hmm

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 6, 2019, 5:43am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/4 "2019-09-06T05:43:59Z")

</div>

> [@xiaodai](#):
>
> getT(x) # shoud

it can be done inplace? or you need a new vector?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 6, 2019, 5:45am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/5 "2019-09-06T05:45:27Z")

</div>

Need a new vector.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 6, 2019, 6:38am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/6 "2019-09-06T06:38:49Z")

</div>

the fastest way that i found was to create a default type \<: Real and overload the \* operator, so when it encounters a missing, it returns the default val, my code is the following:

```julia

nn = 10000000
struct DefaultNumber{T<: Real} <: Real 
    val::T
end
default_number(x::Real) = DefaultNumber{typeof(x)}(x)

Base.:*(a1::DefaultNumber,a2::Real) = a2
Base.:*(a1::Real,a2::DefaultNumber) = *(a2,a1)
Base.:*(a1::DefaultNumber,a2::Missing) = a1.val
Base.:*(a1::Missing,a2::DefaultNumber) = a2.val

#create vector
x_old = Vector{Union{Int64,Missing}}(undef,nn)
for i in 1:nn
    rand() < 0.8 && (global x_old[i] = rand(Int64))
end

function replace_missing(x_old::T,val::T2) where T<:AbstractVector{Union{T2,Missing}} where T2
    default_val = default_number(val)
    x_new = default_val.*x_old
end

function replace_missing2(x_old::T,val::T2) where T<:AbstractVector{Union{T2,Missing}} where T2
    default_val = default_number(val)
    x_new = Vector{T2}(undef,length(x_old))
    @inbounds @simd for i in 1:length(x_old)
    x_new[i] = default_val*x_old[i]
    end
    return x_new
end

function replace_missing3(x_old::T,val::T2) where T<:AbstractVector{Union{T2,Missing}} where T2
    return collect(Missings.replace(x_old, val))
end

function replace_missing4(x_old::T,val::T2) where T<:AbstractVector{Union{T2,Missing}} where T2
    x_new = Vector{T2}(undef,length(x_old))
    @inbounds @simd for i in 1:length(x_old)
        if !ismissing(x_old[i])
        x_new[i] = x_old[i]
        else
            x_new[i] = val
        end
    end
    return x_new
end

```

```julia
julia> @btime replace_missing(x_old,2) #broadcasting default number
  39.520 ms (2 allocations: 76.29 MiB)

julia> @btime replace_missing2(x_old,2) #for loop with default number
  38.559 ms (2 allocations: 76.29 MiB)

 @btime replace_missing3(x_old,2) #using Missings.collect
  51.644 ms (3 allocations: 76.29 MiB)

julia> @btime replace_missing4(x_old,2) #for loop with if-else
  45.405 ms (2 allocations: 76.29 MiB)

julia> @btime coalesce.(x_old,2) #as suggested by tbeason
  39.303 ms (4 allocations: 76.29 MiB)

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 6, 2019, 6:44am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/7 "2019-09-06T06:44:19Z")

</div>

> [@xiaodai](#):
>
> I think the implementation will do some ifs. BEst to just extract the underlying array of bytes… Hmm

the problem with that is the `Union{Missing,T}` is not a `isbits` type

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [September 6, 2019, 6:51am UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/8 "2019-09-06T06:51:33Z")

</div>

Does `replace!` works for you?

```julia
julia> replace!([1, missing, 2, 3, missing], missing => 0)
5-element Array{Union{Missing, Int64},1}:
 1
 0
 2
 3
 0

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 6, 2019, 4:18pm UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/9 "2019-09-06T16:18:37Z")

</div>

I still don’t see why `coalesce.(x,0)` isn’t what you want…?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 6, 2019, 4:37pm UTC](https://discourse.julialang.org/t/how-do-i-obtain-the-t-values-in-a-vector-vector-union-missing-t/28455/10 "2019-09-06T16:37:22Z")

</div>

> [@rfourquet](#):
>
> ia\> replace!([1, missing, 2, 3, missing], missing =\> 0) 5-element Array{Union{Missing, Int64},1}: 1 0 2 3 0

yeah, is pretty fast too
