use of tech.tablesaw.table.TableSliceGroup in project symja_android_library by axkr.
the class PivotTable method pivot.
public static Table pivot(Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2, NumericColumn<?> values, AggregateFunction<?, ?> aggregateFunction) {
TableSliceGroup tsg = table.splitOn(column1);
Table pivotTable = Table.create("Pivot: " + column1.name() + " x " + column2.name());
pivotTable.addColumns(column1.type().create(column1.name()));
List<String> valueColumnNames = getValueColumnNames(table, column2);
for (String colName : valueColumnNames) {
pivotTable.addColumns(DoubleColumn.create(colName));
}
int valueIndex = table.columnIndex(column2);
int keyIndex = table.columnIndex(column1);
String key;
for (TableSlice slice : tsg.getSlices()) {
key = String.valueOf(slice.get(0, keyIndex));
pivotTable.column(0).appendCell(key);
Map<String, Double> valueMap = getValueMap(column1, column2, values, valueIndex, slice, aggregateFunction);
for (String columnName : valueColumnNames) {
Double aDouble = valueMap.get(columnName);
NumericColumn<?> pivotValueColumn = pivotTable.numberColumn(columnName);
if (aDouble == null) {
pivotValueColumn.appendMissing();
} else {
pivotValueColumn.appendObj(aDouble);
}
}
}
return pivotTable;
}
use of tech.tablesaw.table.TableSliceGroup in project symja_android_library by axkr.
the class Summarizer method having.
public Table having(Function<Table, Selection> selection) {
Preconditions.checkState(groupColumnNames.length > 0, "Cannot perform having() on summary that has not been grouped first");
if (groupColumnNames[0].equals(GROUP_COL_TEMP_NAME)) {
IntColumn groupColumn = temp.intColumn(GROUP_COL_TEMP_NAME);
TableSliceGroup group = StandardTableSliceGroup.create(temp, groupColumn);
return summarizeForHaving(group, selection);
} else {
TableSliceGroup group = StandardTableSliceGroup.create(temp, groupColumnNames);
return summarizeForHaving(group, selection);
}
}
use of tech.tablesaw.table.TableSliceGroup in project symja_android_library by axkr.
the class Summarizer method apply.
/**
* Returns the result of applying to the functions to all the values in the appropriate column
* TODO add a test that uses a non numeric return type with apply
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public Table apply() {
if (groupColumnNames.length > 0) {
TableSliceGroup group = StandardTableSliceGroup.create(temp, groupColumnNames);
return summarize(group);
} else {
List<Table> results = new ArrayList<>();
ArrayListMultimap<String, AggregateFunction<?, ?>> reductionMultimap = getAggregateFunctionMultimap();
for (String name : reductionMultimap.keys()) {
List<AggregateFunction<?, ?>> reductions = reductionMultimap.get(name);
Table table = TableSliceGroup.summaryTableName(temp);
for (AggregateFunction function : reductions) {
Column column = temp.column(name);
Object result = function.summarize(column);
ColumnType type = function.returnType();
Column newColumn = type.create(TableSliceGroup.aggregateColumnName(name, function.functionName()));
if (result instanceof Number) {
Number number = (Number) result;
newColumn.append(number.doubleValue());
} else {
newColumn.append(result);
}
table.addColumns(newColumn);
}
results.add(table);
}
return (combineTables(results));
}
}
Aggregations