# Compute mean of array where all values could be missing

**URL:** <https://discourse.julialang.org/t/compute-mean-of-array-where-all-values-could-be-missing/59754>\
**Category:** New to Julia\
**Created:** [April 21, 2021, 8:16pm UTC](https://discourse.julialang.org/t/compute-mean-of-array-where-all-values-could-be-missing/59754 "2021-04-21T20:16:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elbersb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elbersb/32/26568_2.png) [@elbersb](https://discourse.julialang.org/u/elbersb)\
**Post date:** [April 21, 2021, 8:16pm UTC](https://discourse.julialang.org/t/compute-mean-of-array-where-all-values-could-be-missing/59754/1 "2021-04-21T20:16:04Z")

</div>

I ran into this today:

```julia
using Statsbase
mean(skipmissing([missing]))
> ERROR: ArgumentError: reducing over an empty collection is not allowed
> Stacktrace:
> [1] _empty_reduce_error()
> @ Base ./reduce.jl:299
> [2] reduce_empty(#unused#::typeof(+), #unused#::Core.TypeofBottom)
> @ Base ./reduce.jl:310
> [3] mapreduce_empty(#unused#::typeof(identity), op::Function, T::Type)
> @ Base ./reduce.jl:343
> ...

```

The use case for me is that I have an array where sometimes all values are missing. So I have to do `skipmissing` first, then check whether it’s empty, and then call `mean`. **Is there a way to avoid the branch?**

FWIW, R returns `NaN` in this case.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 21, 2021, 8:31pm UTC](https://discourse.julialang.org/t/compute-mean-of-array-where-all-values-could-be-missing/59754/2 "2021-04-21T20:31:25Z")

</div>

I don’t think there is a good solution for this at the moment, unfortunately.

`NaN` isn’t a great return type in this context, since `mean` might not just apply to numbers. You can take the mean of a `Vector` of `Vector`s, for example.

Maybe you can fix this upstream?

```julia
julia> t = Union{Float64, Missing}[missing, missing, missing]
3-element Vector{Union{Missing, Float64}}:
 missing
 missing
 missing

julia> mean(skipmissing(t))
NaN

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 21, 2021, 8:44pm UTC](https://discourse.julialang.org/t/compute-mean-of-array-where-all-values-could-be-missing/59754/3 "2021-04-21T20:44:23Z")

</div>

Would this work?

```julia
y=[missing missing]
isempty(begin x=skipmissing(y) end) ? x=NaN : x=mean(x)

NaN

y=[1 3]
isempty(begin x=skipmissing(y) end) ? x=NaN : x=mean(x)

2.0

```

---

<div class="post-metadata">

**Author:** ![elbersb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elbersb/32/26568_2.png) [@elbersb](https://discourse.julialang.org/u/elbersb)\
**Post date:** [April 21, 2021, 8:44pm UTC](https://discourse.julialang.org/t/compute-mean-of-array-where-all-values-could-be-missing/59754/4 "2021-04-21T20:44:42Z")

</div>

Thanks! So

```julia
mean(skipmissing(Union{Float64,Missing}[missing]))
> NaN

```

but

```julia
mean(skipmissing(Union{Missing}[missing]))

```

gives the error. So I should make sure to set a union type before calling mean, and it should work. Great!

Regarding the return value – ideally it would be `missing`, no?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 21, 2021, 8:49pm UTC](https://discourse.julialang.org/t/compute-mean-of-array-where-all-values-could-be-missing/59754/5 "2021-04-21T20:49:40Z")

</div>

No, I don’t think the return value would be `missing`. The return value should be the same as `mean(Float64[])`.

I guess so. Or maybe when you read in the data, you should just make sure that if a column is all missing, julia knows that those values _could_ be `Float64`s.

---

<div class="post-metadata">

**Author:** ![elbersb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elbersb/32/26568_2.png) [@elbersb](https://discourse.julialang.org/u/elbersb)\
**Post date:** [April 21, 2021, 8:54pm UTC](https://discourse.julialang.org/t/compute-mean-of-array-where-all-values-could-be-missing/59754/6 "2021-04-21T20:54:48Z")

</div>

> [@pdeffebach](#):
>
> No, I don’t think the return value would be `missing` . The return value should be the same as `mean(Float64[])` .

Got it. That makes sense.

> [@pdeffebach](#):
>
> I guess so. Or maybe when you read in the data, you should just make sure that if a column is all missing, julia knows that those values _could_ be `Float64` s.

The vector is simulation output that I create myself, so I’ll just make sure that there is a type set when I create the vector. Thanks again for the quick help!
