Search in sources :

Example 1 with AggregateFunction

use of tech.tablesaw.aggregate.AggregateFunction in project symja_android_library by axkr.

the class TableSliceGroup method aggregate.

/**
 * Applies the given aggregations to the given columns. The apply and combine steps of a
 * split-apply-combine.
 *
 * @param functions map from column name to aggregation to apply on that function
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Table aggregate(ListMultimap<String, AggregateFunction<?, ?>> functions) {
    Table groupTable = summaryTableName(sourceTable);
    StringColumn groupColumn = StringColumn.create("Group");
    groupTable.addColumns(groupColumn);
    boolean firstFunction = true;
    for (Map.Entry<String, Collection<AggregateFunction<?, ?>>> entry : functions.asMap().entrySet()) {
        String columnName = entry.getKey();
        for (AggregateFunction function : entry.getValue()) {
            String colName = aggregateColumnName(columnName, function.functionName());
            ColumnType type = function.returnType();
            Column resultColumn = type.create(colName);
            for (TableSlice subTable : getSlices()) {
                Object result = function.summarize(subTable.column(columnName));
                if (firstFunction) {
                    groupColumn.append(subTable.name());
                }
                if (function.returnType().equals(ColumnType.DOUBLE)) {
                    Number number = (Number) result;
                    resultColumn.append(number.doubleValue());
                } else {
                    resultColumn.append(result);
                }
            }
            groupTable.addColumns(resultColumn);
            firstFunction = false;
        }
    }
    return splitGroupingColumn(groupTable);
}
Also used : StringColumn(tech.tablesaw.api.StringColumn) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) StringColumn(tech.tablesaw.api.StringColumn) Column(tech.tablesaw.columns.Column) AggregateFunction(tech.tablesaw.aggregate.AggregateFunction) Collection(java.util.Collection) Map(java.util.Map)

Aggregations

Collection (java.util.Collection)1 Map (java.util.Map)1 AggregateFunction (tech.tablesaw.aggregate.AggregateFunction)1 ColumnType (tech.tablesaw.api.ColumnType)1 StringColumn (tech.tablesaw.api.StringColumn)1 Table (tech.tablesaw.api.Table)1 Column (tech.tablesaw.columns.Column)1