Mean

Mean( arrayOrSet )
Mean( arrayOrSet )
Mean( arrayOrSet )
Mean( arrayOrSet )
mean( arrayOrSet )
Mean( arrayOrSet )

Description

The Mean function returns the average value of the items in arrayOrSet, which is an Array, Page, or Set. The mean is calculated by dividing the sum of the arrayOrSet's values by the count of the arrayOrSet's values.

The run time of Mean is dependent on the number of elements in the underlying set or page — it’s linear, or O(n). For very large sets or pages, executing Mean might result in a query timeout error, or "width" error.

For query "width" errors, the underlying set or page involves more than 100K items. This can happen when using a set function, such as Difference, where more than 100K items need to be considered to produce the set that Mean evaluates. To resolve this, use Paginate to limit the set or page size.

For example, instead of:

Mean(
  Difference(
    Match(Index("Index1"), "term1"),
    Match(Index("Index2"), "term2")
  )
)

use:

Mean(
  Paginate(
    Difference(
      Match(Index("Index1"), "term1"),
      Match(Index("Index2"), "term2")
    ),
    { size: 10000 }
  )
)

This does mean that if the entire set must be evaluated to arrive at the correct result, you would have to page through the Paginate results.

For query timeout errors, you may specify a larger query timeout via the driver that you are using.

Parameters

Argument Type Definition and Requirements

arrayOrSet

Array, Page, or Set

The array, page, or set that should have its average value calculated.

Returns

A Number representing the average value of all items in arrayOrSet.

Examples

The following query returns the average of the items in the provided array:

try
{
    Value result = await client.Query(
        Mean(Arr(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
DoubleV(5.5)
result, err := client.Query(
	f.Mean(f.Arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
5.5
System.out.println(
    client.query(
        Mean(
            Arr(
                Value(1), Value(2), Value(3), Value(4), Value(5),
                Value(6), Value(7), Value(8), Value(9), Value(10)
            )
        )
    ).get()
);
5.5
client.query(
  q.Mean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
5.5
result = client.query(
  q.mean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)
print(result)
5.5
try {
  println(Await.result(
  client.query(
    Mean(Arr(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
  ),
  5.seconds)
)} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
5.5

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!