# How do I calculate percentiles/IQR with julia?

**URL:** <https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 27, 2019, 7:20am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635 "2019-05-27T07:20:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [May 27, 2019, 7:20am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635/1 "2019-05-27T07:20:36Z")

</div>

Hello,  
I would like to find the percentile and interquartile range of a column of a dataframe.  
I have loaded both Statistics and StatsBase, but when I try `iqr(N[:column])` I get:

```julia
ERROR: MethodError: no method matching iqr(::Array{Union{Missing, Int64},1})
Closest candidates are:
  iqr(::AbstractArray{#s77,N} where N where #s77<:Real) at ~/.julia/packages/StatsBase/XePnn/src/scalarstats.jl:288
Stacktrace:
 [1] top-level scope at none:0

```

and similarly,

```julia
percentile(N[:column], 25)
ERROR: MethodError: no method matching percentile(::Array{Union{Missing, Int64},1}, ::Int64)
Closest candidates are:
  percentile(::AbstractArray{#s35,N} where N where #s35<:Real, ::Any) at ~/.julia/packages/StatsBase/XePnn/src/scalarstats.jl:174
Stacktrace:
 [1] top-level scope at none:0

```

Using the help function of REPL I got:

```julia
help?> iqr
search: iqr isqrt PartialQuickSort

  iqr(v)

  Compute the interquartile range (IQR) of an array, i.e. the 75th percentile minus the 25th percentile.
help?> percentile
search: percentile

  percentile(v, p)

  Return the pth percentile of a real-valued array v, i.e. quantile(x, p / 100).

```

Could these stats be computed on a dataframe? What am I getting wrong?  
Thank you

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 27, 2019, 7:26am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635/2 "2019-05-27T07:26:42Z")

</div>

Remove the missing values, eg `collect(skipmissing(N[:column)))`.

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [May 27, 2019, 7:34am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635/3 "2019-05-27T07:34:35Z")

</div>

There are no missing values:

```julia
julia> unique(df[:column]))
8-element Array{Union{Missing, Int64},1}:
 6064657
 6064658
 6064659
 6064660
 6064662
 6064666
 6064667
 6064668

```

and

```julia
julia> println(df[:column])
Union{Missing, Int64}[6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064657, 6064658, 6064659, 6064659, 6064660, 6064662, 6064666, 6064667, 6064668]

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 27, 2019, 8:04am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635/4 "2019-05-27T08:04:47Z")

</div>

In that case, my suggestion would only changes the element type. This is needed because of

[https://github.com/JuliaStats/StatsBase.jl/issues/449](https://github.com/JuliaStats/StatsBase.jl/issues/449)

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [May 27, 2019, 8:29am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635/5 "2019-05-27T08:29:37Z")

</div>

Here’s an example:

```julia
D1 = DataFrame(a=randn(100))
allowmissing!(D1)
iqr(D1.a) # error
disallowmissing!(D1)
iqr(D1.a) # 1.517

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [May 27, 2019, 8:56am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635/6 "2019-05-27T08:56:37Z")

</div>

> [@Mattriks](#):
>
> disallowmissing!(D1)

That is nice, but I get the error `MethodError: no method matching iterate(::DataFrame)`. Is it part of a specific package? REPL’s help only reports `disallowmissing(x::AbstractArray)...` and there was not much stuff on internet.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [May 27, 2019, 9:37am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635/7 "2019-05-27T09:37:10Z")

</div>

disallowmissing! is in the [DataFrames](http://juliadata.github.io/DataFrames.jl/stable/lib/functions.html) package  
Try either:

```julia
?disallowmissing!

methods(disallowmissing!)

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [May 27, 2019, 10:08am UTC](https://discourse.julialang.org/t/how-do-i-calculate-percentiles-iqr-with-julia/24635/8 "2019-05-27T10:08:47Z")

</div>

That is weird then, since I loaded DataFrames already…
