mean

Description

Computes the mean (arithmetic average) of an Observable.

API

mean([initialState={average: 0, sum: 0, index: 0}])

Since

0.5

Parameters

None

Options

  • initialState: Object {average: Number, sum: Number, index: Number: Sets a warm start value so that the mean calculation can continue from a non-zero starting point (instead of a blank state).

Returns

Number. (The current mean of the Observable.)

Examples

Basic Example

Skipping the first value

It's odd to calculate the mean of just one item. Sometimes, it makes more sense to skip the first item to ensure that means are calculated based on more than one number. RxJS makes this easy:

Hotstart

Calculate a mean with an initial warmstart value (instead starting with a blank state):

Using Hotstarts to aggregate multiple streams

Sometimes it is useful to be able to compute a mean from multiple data streams without processing the individual items from each stream. rxjs-stats enables this too. Suppose you have data points from 3 different users' browser applications. They can be combined into a single mean that represents all three data streams:

In the example above, aggregateMean$ computes the mean of the data points from all three users without viewing any of their individual information!

Last updated

Was this helpful?