quantile function Summarizing data
Returns the p
-quantile of all values in the iterable
, where p
is a
number in the range [0, 1].
For example, the median can be computed using p
= 0.5, the first quartile
at p
= 0.25, and the third quartile at p
= 0.75.
This particular implementation uses the R-7 method, which is the default for the R programming language and Excel.
This function ignores values that do not satisfy any of the following conditions:
- The value is not
null
. - The value is not double.nan.
Useful for filtering and ignoring missing data in datasets.
If the iterable
is empty or contains no valid values, this function
returns null
.
Implementation
num? quantile(Iterable<num?> iterable, num p) {
var elements = numbers(iterable), n = elements.length;
if (n == 0 || p.isNaN) return null;
if (p <= 0 || n < 2) return min(elements);
if (p >= 1) return max(elements);
var i = (n - 1) * p,
i0 = i.floor(),
value0 = max(quickselect(elements, i0).sublist(0, i0 + 1))!,
value1 = min(elements.sublist(i0 + 1))!;
return value0 + (value1 - value0) * (i - i0);
}