# Dipatch on AbstractArray which contains missing values

**URL:** https://discourse.julialang.org/t/dipatch-on-abstractarray-which-contains-missing-values/83598
**Category:** New to Julia
**Created:** [July 1, 2022, 5:27am UTC](https://discourse.julialang.org/t/dipatch-on-abstractarray-which-contains-missing-values/83598 "2022-07-01T05:27:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)
#### Post date: [July 1, 2022, 5:27am UTC](https://discourse.julialang.org/t/dipatch-on-abstractarray-which-contains-missing-values/83598/1 "2022-07-01T05:27:24Z")

</div>

I have a Vector:

```julia
a_vec=[missing, 1, 2, 3]

```

When I need to calculate the mean of `a_vec`, I used to do:

```julia
mean(skipmissing(a_vec))
2

```

This is the most situation I met. So I would like to `skipmissing` by default, I would like to do:

```julia
Base.mean(x) = mean(skipmissing(x))

```

But the `skipmissing` call consume time, So I have to do:

```julia
Base.mean(x::AbstractArray{T,1}) where {Missing <: T}=mean(skipmissing(x))

UndefVarError: T not defined

```

In another way, I could do it by:

```julia
Base.mean(x::AbstractArray{T,1}) where {T<:Number,1}=mean(x)
Base.mean(x)=mean(skipmissing(x))

```

But when I use `sum`, `var` etc…, have to do it again and again.

So what should I do to dispath method on `AbstractArray` containing `missing`?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 1, 2022, 5:55am UTC](https://discourse.julialang.org/t/dipatch-on-abstractarray-which-contains-missing-values/83598/2 "2022-07-01T05:55:06Z")

</div>

Two side notes (I’m on a mobile): first the “call” itself to skipmissing is likelly to be negligible compared with the rest of your operations, and secondly what you are trying to do (override a method for a type you don’t own) is considered very bad practice and goes under the name of “type piracy”.  
Think if you write a package that override Base.mean and then someone use your package .  
Then when he/she compute the mean for even unrelated things he/she will have the modified behaviour without knowing it!

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [July 1, 2022, 7:00am UTC](https://discourse.julialang.org/t/dipatch-on-abstractarray-which-contains-missing-values/83598/3 "2022-07-01T07:00:07Z")

</div>

In addtion to sylvaticus’ very important points, I have two comments

1. The stats functions like `mean` etc. are not in `Base` but in `Statistics`
2. The `T` in your dispatch should be `Union{Missing, T}`

But IMHO it’s really much simpler and clearer to be explicit about it at the expense of a few extra characters

```julia
import Base: skipmissing as sm
mean(sm([missing,1])) = 1.0

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 1, 2022, 7:07am UTC](https://discourse.julialang.org/t/dipatch-on-abstractarray-which-contains-missing-values/83598/4 "2022-07-01T07:07:36Z")

</div>

> [@Brian1](#):
>
> But the `skipmissing` call consume time, So I have to do:

It shouldn’t make any difference whether you call `mean(skipmissing(x))` or change the definition of `mean` itself, like you are trying. It still calls `skipmissing`.

> [@Brian1](#):
>
> `Base.mean(x) = mean(skipmissing(x))`

BTW, this definition is recursive.

---

<div class="post-metadata">

### Author: ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)
#### Post date: [July 1, 2022, 7:11am UTC](https://discourse.julialang.org/t/dipatch-on-abstractarray-which-contains-missing-values/83598/5 "2022-07-01T07:11:18Z")

</div>

Thank you for your reply and advice. Acturelly, in my local code, the code seems like

```julia
Statistics.quantile(x::AA{U{Mi,T},1},y) where {T}=quantile(smiss(x),y)
#=
const AA = AbstractArray
const Mi = Missing
const U = Union
=#

```
