CrossTab { private static Table tablePercents(Table xTabCounts) { Table pctTable = Table.create("Crosstab Table Proportions: "); StringColumn labels = StringColumn.create(LABEL_COLUMN_NAME); pctTable.addColumns(labels); double grandTotal = xTabCounts.numberColumn(xTabCounts.columnCount() - 1).getDouble(xTabCounts.rowCount() - 1); for (int i = 0; i < xTabCounts.rowCount(); i++) { labels.append(xTabCounts.column(0).getString(i)); } DoubleColumn[] newColumns = new DoubleColumn[xTabCounts.columnCount() - 1]; for (int i = 1; i < xTabCounts.columnCount(); i++) { Column<?> column = xTabCounts.column(i); newColumns[i - 1] = DoubleColumn.create(column.name()); } for (int i = 0; i < xTabCounts.rowCount(); i++) { for (int c = 0; c < newColumns.length; c++) { if (grandTotal == 0) { newColumns[c].append(Double.NaN); } else { newColumns[c].append(xTabCounts.numberColumn(c + 1).getDouble(i) / grandTotal); } } } pctTable.addColumns(newColumns); return pctTable; } private  CrossTab(); static Table counts(
      Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2); static Table counts(Table table, String columnName); static Table percents(Table table, String column1); static Table columnPercents(
      Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2); static Table columnPercents(Table table, String column1, String column2); static Table rowPercents(
      Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2); static Table rowPercents(Table table, String column1, String column2); static Table tablePercents(
      Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2); static Table tablePercents(Table table, String column1, String column2);  }
@Test public void testTablePercents() throws Exception { Table bush = Table.read().csv("../data/bush.csv"); bush.addColumns(bush.dateColumn("date").year()); Table xtab = CrossTab.tablePercents(bush, "who", "date year"); assertEquals( 1.0, xtab.doubleColumn(xtab.columnCount() - 1).getDouble(xtab.rowCount() - 1), 0.00001); }