use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class TableBuildingUtils method build.
public static Table build(List<String> columnNames, List<String[]> dataRows, ReadOptions options) {
Table table = Table.create(options.tableName());
if (dataRows.isEmpty()) {
return table;
}
ColumnTypeDetector detector = new ColumnTypeDetector(options.columnTypesToDetect());
Iterator<String[]> iterator = dataRows.iterator();
ColumnType[] types = detector.detectColumnTypes(iterator, options);
// If there are columnTypes configured by the user use them
for (int i = 0; i < types.length; i++) {
boolean hasColumnName = i < columnNames.size();
Optional<ColumnType> configuredColumnType = options.columnTypeReadOptions().columnType(i, hasColumnName ? columnNames.get(i) : null);
if (configuredColumnType.isPresent())
types[i] = configuredColumnType.get();
}
for (int i = 0; i < columnNames.size(); i++) {
table.addColumns(types[i].create(columnNames.get(i)));
}
for (int i = 0; i < dataRows.size(); i++) {
for (int j = 0; j < table.columnCount(); j++) {
table.column(j).appendCell(dataRows.get(i)[j]);
}
}
return table;
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class SerializableDataSetTest method testDataset.
public void testDataset() {
Table table = Table.read().csv(//
"Products,Sales,Market_Share\n" + //
"a,5500,3\n" + //
"b,12200,4\n" + "c,60000,33\n", "");
ASTDataset ds = ASTDataset.newTablesawTable(table);
equalsStringCopy(ds);
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class CrossTab method rowPercents.
private static Table rowPercents(Table xTabCounts) {
Table pctTable = Table.create("Crosstab Row Proportions: ");
StringColumn labels = StringColumn.create(LABEL_COLUMN_NAME);
pctTable.addColumns(labels);
for (int i = 0; i < xTabCounts.rowCount(); i++) {
labels.append(xTabCounts.column(0).getString(i));
}
// create the new cols
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++) {
double rowTotal = xTabCounts.numberColumn(xTabCounts.columnCount() - 1).getDouble(i);
for (int c = 0; c < newColumns.length; c++) {
if (rowTotal == 0) {
newColumns[c].append(Double.NaN);
} else {
newColumns[c].append(xTabCounts.numberColumn(c + 1).getDouble(i) / rowTotal);
}
}
}
pctTable.addColumns(newColumns);
return pctTable;
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class CrossTab method counts.
/**
* Returns a table containing two-dimensional cross-tabulated counts for each combination of
* values in {@code column1} and {@code column2}
*
* <p>
*
* @param table The table we're deriving the counts from
* @param column1 A column in {@code table}
* @param column2 Another column in {@code table}
* @return A table containing the cross-tabs
*/
public static Table counts(Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2) {
Table t = Table.create("Crosstab Counts: " + column1.name() + " x " + column2.name());
t.addColumns(column1.type().create(LABEL_COLUMN_NAME));
Table temp = table.sortOn(column1.name(), column2.name());
int colIndex1 = table.columnIndex(column1.name());
int colIndex2 = table.columnIndex(column2.name());
com.google.common.collect.Table<String, String, Integer> gTable = TreeBasedTable.create();
String a;
String b;
for (int row = 0; row < table.rowCount(); row++) {
a = temp.column(colIndex1).getString(row);
b = temp.column(colIndex2).getString(row);
Integer cellValue = gTable.get(a, b);
Integer value;
if (cellValue != null) {
value = cellValue + 1;
} else {
value = 1;
}
gTable.put(a, b, value);
}
for (String colName : gTable.columnKeySet()) {
t.addColumns(IntColumn.create(colName));
}
t.addColumns(IntColumn.create("total"));
int[] columnTotals = new int[t.columnCount()];
for (String rowKey : gTable.rowKeySet()) {
t.column(0).appendCell(rowKey);
int rowSum = 0;
for (String colKey : gTable.columnKeySet()) {
Integer cellValue = gTable.get(rowKey, colKey);
if (cellValue != null) {
int colIdx = t.columnIndex(colKey);
t.intColumn(colIdx).append(cellValue);
rowSum += cellValue;
columnTotals[colIdx] = columnTotals[colIdx] + cellValue;
} else {
t.intColumn(colKey).append(0);
}
}
t.intColumn(t.columnCount() - 1).append(rowSum);
}
if (t.column(0).type().equals(ColumnType.STRING)) {
t.column(0).appendCell("Total");
} else {
t.column(0).appendCell("");
}
int grandTotal = 0;
for (int i = 1; i < t.columnCount() - 1; i++) {
t.intColumn(i).append(columnTotals[i]);
grandTotal = grandTotal + columnTotals[i];
}
t.intColumn(t.columnCount() - 1).append(grandTotal);
return t;
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class XlsxReader method createTable.
private Table createTable(Sheet sheet, TableRange tableArea, XlsxReadOptions options) {
Optional<List<String>> optHeaderNames = getHeaderNames(sheet, tableArea);
optHeaderNames.ifPresent(h -> tableArea.startRow++);
List<String> headerNames = optHeaderNames.orElse(calculateDefaultColumnNames(tableArea));
Table table = Table.create(options.tableName() + "#" + sheet.getSheetName());
List<Column<?>> columns = new ArrayList<>(Collections.nCopies(headerNames.size(), null));
for (int rowNum = tableArea.startRow; rowNum <= tableArea.endRow; rowNum++) {
Row row = sheet.getRow(rowNum);
for (int colNum = 0; colNum < headerNames.size(); colNum++) {
int excelColNum = colNum + tableArea.startColumn;
Cell cell = row.getCell(excelColNum, MissingCellPolicy.RETURN_BLANK_AS_NULL);
Column<?> column = columns.get(colNum);
String columnName = headerNames.get(colNum);
if (cell != null) {
if (column == null) {
column = createColumn(colNum, columnName, sheet, excelColNum, tableArea, options);
columns.set(colNum, column);
while (column.size() < rowNum - tableArea.startRow) {
column.appendMissing();
}
}
Column<?> altColumn = appendValue(column, cell);
if (altColumn != null && altColumn != column) {
column = altColumn;
columns.set(colNum, column);
}
} else {
boolean hasCustomizedType = options.columnTypeReadOptions().columnType(colNum, columnName).isPresent();
if (column == null && hasCustomizedType) {
ColumnType columnType = options.columnTypeReadOptions().columnType(colNum, columnName).get();
column = columnType.create(columnName).appendMissing();
columns.set(colNum, column);
} else if (hasCustomizedType) {
column.appendMissing();
}
}
if (column != null) {
while (column.size() <= rowNum - tableArea.startRow) {
column.appendMissing();
}
}
}
}
columns.removeAll(Collections.singleton(null));
table.addColumns(columns.toArray(new Column<?>[columns.size()]));
return table;
}
Aggregations