MedianDocs
The Median Operation
Computes the median of values: the value in the "middle" of the sorted values. This is also called the 50th percentile, or the 2-quantile.
Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.
null values in the input are always ignored.
If the input contains values, the result will be NaNNaN, unless skipNaN is set to true.
For primitive numbers, the resulting median is a Double, interpolated between the values using quantile estimation method R8. Converting Long values to Double may lose precision for very large values. For all other self-comparable values, the resulting median is selected from the values using quantile estimation method R3, so it has the same type as those values.
Currently, only the default quantile estimation methods can be used. In the future, it might become possible to choose a different one, see #1121.
For more information about the resulting types: See "median Type Conversion" on the documentation website.
Big numbers (, BigInteger) are not explicitly supported. They are self-comparable, so some overloads select a value without interpolating it, while others throw an exception at runtime. Don't rely on this; BigDecimal them to a primitive number type first.convert
Median Modes
Depending on what exactly you want the median of, there are several modes. They are shown here for DataFrame, but they exist for the other receivers too:
median()— the median of each suitable column separately.median{ columns }— a single median of all values in all selected columns.medianFor{ columns }— the median of each selected column separately.medianOf{ expression }— the median of the values that the given expression returns for each row.medianBy{ expression }— the row at the median of the values that the given expression returns for each row.
median{ columns }, , and medianOf all have an medianBy-OrNull counterpart which returns null instead of throwing an exception when there is nothing to compute the median of.
Due to a limitation in Kotlin's overload resolution (KT-76683), computing the median of non-number comparable values with median{ columns } or requires either explicit type arguments, like medianOfdf.median<_, String> { name.firstName }, or passing the lambda inside the parentheses, like df.median({ name.firstName }).
From Kotlin 2.5, this limitation can be lifted by enabling eager lambda analysis (KT-51107) with the compiler option -XXLanguage:+EagerLambdaAnalysis, like kotlin { compilerOptions { freeCompilerArgs.add("-XXLanguage:+EagerLambdaAnalysis") } } in Gradle. Then, the right overload is picked for lambdas returning non-number comparable values too, without explicit type arguments. Note that this is an internal compiler option without any stability guarantees, so it is not recommended for production use yet.
Related operation:
— any percentile of values;percentilemedianis the same aspercentile(50.0).
For more information: See median on the documentation website.
See all summary statistics: See "Summary statistics" on the documentation website.