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

mean() : Calculate Mean (Average)


On this page

Summary

Calculate the arithmetic mean of numeric values in a collection. This is a mathematical synonym for the average() function.

Signature

mean(collection)

Parameters

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

Returns

Type: Number (Decimal)

The arithmetic mean of all values. Returns null for empty collections.


Examples

Basic Mean Calculation

<p>Mean temperature: °F</p>

Data:

doc.Params["model"] = new {
    temperatures = new[] { 72, 68, 75, 70, 73 }
};

Output:

<p>Mean temperature: 71.6°F</p>

Statistical Summary

<h3>Data Analysis</h3>
<p>Mean: </p>
<p>Median: </p>
<p>Min: </p>
<p>Max: </p>

Data:

doc.Params["model"] = new {
    values = new[] { 10, 15, 20, 25, 30, 35, 40 }
};

Output:

<h3>Data Analysis</h3>
<p>Mean: 25.00</p>
<p>Median: 25.00</p>
<p>Min: 10</p>
<p>Max: 40</p>

Comparing Mean and Median

<h3>Score Distribution</h3>
<p>Mean score: </p>
<p>Median score: </p>

  <p style="color: orange;">Note: Large difference suggests outliers in data</p>

Data:

doc.Params["model"] = new {
    scores = new[] { 85, 87, 90, 92, 95, 45 }  // 45 is an outlier
};

Output:

<h3>Score Distribution</h3>
<p>Mean score: 82.3</p>
<p>Median score: 88.5</p>
<p style="color: orange;">Note: Large difference suggests outliers in data</p>

Property-Based Mean

<p>Mean rating:  stars</p>

Data:

doc.Params["model"] = new {
    reviews = new[] {
        new { author = "Alice", rating = 5 },
        new { author = "Bob", rating = 4 },
        new { author = "Charlie", rating = 5 },
        new { author = "Diana", rating = 3 }
    }
};

Output:

<p>Mean rating: 4.2 stars</p>

Notes

  • Identical functionality to average() function
  • Mathematical terminology preferred in statistical contexts
  • Returns null for empty or null collections
  • All items must be numeric
  • Result is a decimal (use round() for formatting)
  • Formula: sum of all values divided by count
  • For property-based means, combine with collect(): mean(collect(items, 'property'))
  • Useful for:
    • Statistical reports
    • Scientific calculations
    • Academic grading
    • Data analysis
    • Quality metrics
  • Sensitive to outliers (extreme values affect the result)
  • Consider using median() when outliers are present
  • Use average() for more intuitive business contexts

See Also