Aggregating vectors and matrices

The same methods that are available for aggregating over the columns of data frames are also available for vectors and matrices.

Aggregating lists

A ListVector<T> contains variable-length lists. Its aggregation methods return one result per list and retain the list index. By default, they follow the vector's missing-value awareness policy, just as ordinary vector aggregation does. Enable missing-value awareness with MayHaveMissingValues or supply an explicit skipMissingValues argument to override the policy for one operation.

When missing elements are omitted, the list [18, 19, NaN, 20] has valid count 3, sum 57, and mean 19. An empty or entirely missing list has count and sum zero and a missing mean. A genuine zero remains an observation. Stored list lengths always include missing elements and do not depend on the aggregation policy. Copies from vectors or matrices and flattened views retain the source's missing-value awareness policy.

Aggregating over matrices

For matrices, it is possible to aggregate over both the rows and columns. The AggregateRows method aggregates the rows of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateRowsBy method aggregates groups within each row. In the following example, we create a matrix of random numbers and compute the means of its rows. We then create a moving window of length 5, and construct a matrix that contains the moving average of each row:

C#
var m1 = Matrix.Random(10, 100);
var rowMeans = m1.AggregateRows(Aggregators.Mean);
var window = Grouping.Window(m1.ColumnCount, 5);
var rowMAs = m1.AggregateRowsBy(window, Aggregators.Mean);

The AggregateColumns method aggregates the columns of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateColumnsBy method aggregates groups within each column. The same example as above, but aggregating over columns instead of rows, becomes:

C#
var m2 = Matrix.Random(100, 10);
var columnMeans = m2.AggregateColumns(Aggregators.Mean);
window = Grouping.Window(m2.RowCount, 5);
var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);

There is one more method for aggregating over columns: AggregateColumnsListwise. This method is the same as the regular AggregateColumns. method with the following exception: whenever a row contains a missing value, the entire row is treated as missing. This effectively means that the aggregation is performed after first performing listwise deletion of rows with missing values. In the next example, we first set some missing values and then compute the listwise column means of the matrix:

C#
var m2 = Matrix.Random(100, 10);
var columnMeans = m2.AggregateColumns(Aggregators.Mean);
window = Grouping.Window(m2.RowCount, 5);
var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);

Aggregating over vectors

Similarly, vectors have Aggregate and AggregateBy methods that aggregate over the entire vector or each group, respectively. In the next example, we create a vector of random values and compute its kurtosis. We then compute the kurtosis of each chunk of length 10:

C#
var v = Vector.Random(100);
var K = v.Aggregate(Aggregators.Kurtosis);
var partition = Grouping.Partition(v.Length, 10);
var Ks = v.AggregateBy(partition, Aggregators.Kurtosis);

The AggregateColumns method aggregates the columns of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateColumnsBy method aggregates groups within each column. The same example as above, but aggregating over columns instead of rows, becomes:

C#
var m2 = Matrix.Random(100, 10);
var columnMeans = m2.AggregateColumns(Aggregators.Mean);
window = Grouping.Window(m2.RowCount, 5);
var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);

There is one more method for aggregating over columns: AggregateColumnsListwise. This method is the same as the regular AggregateColumns. method with the following exception: whenever a row contains a missing value, the entire row is treated as missing. This effectively means that the aggregation is performed after first performing listwise deletion of rows with missing values. In the next example, we first set some missing values and then compute the listwise column means of the matrix:

C#
var m2 = Matrix.Random(100, 10);
var columnMeans = m2.AggregateColumns(Aggregators.Mean);
window = Grouping.Window(m2.RowCount, 5);
var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);