# StatsBase.summarystats not defined for Rational numbers

**URL:** <https://discourse.julialang.org/t/statsbase-summarystats-not-defined-for-rational-numbers/55165>\
**Category:** General Usage\
**Tags:** statistics, rationals\
**Created:** [February 12, 2021, 6:29pm UTC](https://discourse.julialang.org/t/statsbase-summarystats-not-defined-for-rational-numbers/55165 "2021-02-12T18:29:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluesmoon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bluesmoon/32/327_2.png) [@bluesmoon](https://discourse.julialang.org/u/bluesmoon)\
**Post date:** [February 12, 2021, 6:29pm UTC](https://discourse.julialang.org/t/statsbase-summarystats-not-defined-for-rational-numbers/55165/1 "2021-02-12T18:29:20Z")

</div>

Hi,

I have code that uses `StatsBase.summarystats` on a `Vector{T} where T <: Real`

sometimes `T` can be `Rational`, and `summarystats` fails as it is not defined on `Rational`.

Is there an alternative I can use?

My current workaround is to use this block:

```julia
if eltype(data) <: Rational
    summstats = summarystats(float.(data))
    q1 = rationalize(summstats.q25)
    q3 = rationalize(summstats.q75)
else
    summstats = summarystats(data)
    q1 = summstats.q25
    q3 = summstats.q75
end

```

But I was wondering if there was a julian way to do it without the `if/else` branching.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 14, 2021, 10:23am UTC](https://discourse.julialang.org/t/statsbase-summarystats-not-defined-for-rational-numbers/55165/2 "2021-02-14T10:23:53Z")

</div>

I guess you could locally define the method if that’s what you want it to do:

```julia
ulia> import StatsBase:summarystats

julia> summarystats(x::Vector{<:Rational}) = summarystats(float.(x))
summarystats (generic function with 2 methods)

julia> data = Rational.(rand(10))
10-element Vector{Rational{Int64}}:
 2128900019238815//2251799813685248
   49740005378731//1125899906842624
  334714768863577//1125899906842624
 1998648540630853//4503599627370496
 1894366034501649//4503599627370496
 4093457228645821//4503599627370496
  186443106130015//281474976710656
 2069399109862347//2251799813685248
 1119960256153467//1125899906842624
  614936132122229//2251799813685248

julia> summarystats(data)
Summary Stats:
Length: 10
Missing Count: 0
Mean: 0.590943
Minimum: 0.044178
1st Quartile: 0.328123
Median: 0.553084
3rd Quartile: 0.916481
Maximum: 0.994725

```

but this is [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy) so proceed with caution and don’t do this in library code.

I haven’t thought about potential complications of defining this method in general but it might be worth opening an issue to discuss with maintainers whether this should maybe be added?

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [February 14, 2021, 11:35am UTC](https://discourse.julialang.org/t/statsbase-summarystats-not-defined-for-rational-numbers/55165/3 "2021-02-14T11:35:06Z")

</div>

> [@bluesmoon](#):
>
> But I was wondering if there was a julian way to do it without the `if/else` branching.

The `quantile` function seems to work fine with rationals, so you can implement your block simply as `q1, q3 = quantile(data, [1//4, 3//4])`.

```julia
julia> quantile(rand(50),[1//4,3//4])
2-element Array{Float64,1}:
 0.2188261621212782
 0.8143315574024831

julia> quantile(rand(1:20,50).//rand(1:20,50), [1//4,3//4])
2-element Array{Rational{Int64},1}:
 11//24
 23//12

```

An issue to `StatsBase` might be worthwhile anyway. I see no reason why `summarystats` shouldn’t supports rationals too.

---

<div class="post-metadata">

**Author:** ![bluesmoon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bluesmoon/32/327_2.png) [@bluesmoon](https://discourse.julialang.org/u/bluesmoon)\
**Post date:** [March 1, 2021, 7:11pm UTC](https://discourse.julialang.org/t/statsbase-summarystats-not-defined-for-rational-numbers/55165/4 "2021-03-01T19:11:29Z")

</div>

It turns out that this only works if the percentile values are also Rational. If my `data` is Rational but the percentile values are Float64, then the result is also Float64. Still, this is a good idea.
