Skip to main content Link Search Menu Expand Document (external link)

median() : Calculate Median Value


On this page

Summary

Calculate the median (middle value) of numeric values in a collection. The median is the value that separates the higher half from the lower half of the data.

Signature

median(collection)

Parameters

Parameter Type Required Description
collection Array of Numbers Yes The collection of numeric values

Returns

Type: Number

The median value. For even-length collections, returns the average of the two middle values. Returns null for empty collections.


Examples

Basic Median

<p>Median score: </p>

Data:

doc.Params["model"] = new {
    scores = new[] { 78, 82, 85, 88, 92 }  // Odd count: middle is 85
};

Output:

<p>Median score: 85</p>

Even Count Median

<p>Median value: </p>

Data:

doc.Params["model"] = new {
    values = new[] { 10, 20, 30, 40 }  // Even count: (20 + 30) / 2 = 25
};

Output:

<p>Median value: 25</p>

Robust Against Outliers

<h3>Price Analysis</h3>
<p>Mean price: $</p>
<p>Median price: $</p>
<p style="color: gray;">Median is less affected by extreme values</p>

Data:

doc.Params["model"] = new {
    prices = new[] { 10.00, 12.50, 15.00, 18.00, 95.00 }  // 95.00 is outlier
};

Output:

<h3>Price Analysis</h3>
<p>Mean price: $30.10</p>
<p>Median price: $15.00</p>
<p style="color: gray;">Median is less affected by extreme values</p>

Income Distribution

<h3>Household Income Statistics</h3>
<p>Median income: $</p>
<p>Mean income: $</p>

  <p style="color: blue;">Income distribution is right-skewed (high earners pull mean up)</p>

Data:

doc.Params["model"] = new {
    households = new[] {
        new { id = 1, income = 45000 },
        new { id = 2, income = 52000 },
        new { id = 3, income = 48000 },
        new { id = 4, income = 150000 },  // High earner
        new { id = 5, income = 50000 }
    }
};

Output:

<h3>Household Income Statistics</h3>
<p>Median income: $50000</p>
<p>Mean income: $69000</p>
<p style="color: blue;">Income distribution is right-skewed (high earners pull mean up)</p>

Statistical Comparison

<h3>Test Score Analysis</h3>
<p>Number of students: </p>
<p>Mean: </p>
<p>Median: </p>
<p>Mode: </p>
<p>Range:  - </p>

Data:

doc.Params["model"] = new {
    scores = new[] { 85, 90, 85, 92, 88, 85, 95, 78 }
};

Output:

<h3>Test Score Analysis</h3>
<p>Number of students: 8</p>
<p>Mean: 87.2</p>
<p>Median: 86.5</p>
<p>Mode: 85</p>
<p>Range: 78 - 95</p>

Notes

  • Returns null for empty or null collections
  • Automatically sorts values internally (no need to pre-sort)
  • For odd-length collections: returns the exact middle value
  • For even-length collections: returns average of two middle values
  • Not affected by extreme outliers (robust statistic)
  • All items must be numeric
  • Useful for:
    • Income/salary statistics
    • Real estate pricing (median home price)
    • Test scores with outliers
    • Any dataset with extreme values
    • Understanding typical values
  • Preferred over mean() when:
    • Data has outliers
    • Distribution is skewed
    • You want the “typical” value
  • For property-based median, use with collect(): median(collect(items, 'property'))

See Also