Class ColumnsToRowsTransform
There are times when you have a wide table, that is better displayed to the user as a narrow table with additional rows. For example, you might have a table with columns for "Bid", "Ask" and "Last", which you may prefer to have three rows, one each for Bid, Ask, and Last with a label for each symbol.
The same can be accomplished by calling
.update("Label=new String[]{`Bid`, `Ask`, `Last`}", "Value=new double[]{Bid, Ask, Last}").ungroup()
, but
the creation of arrays in the update statement introduces additional overhead and garbage creation to the query
execution.
You may have only a single label column, but you may define multiple output value columns, all of which must have the same number of source columns.
For each output value column, all of the constituent input columns columns must have the same type. If the types are different, then an IllegalArgumentException is thrown.
For example, when calling @{code ColumnsToRowsTransform.columnsToRows(inTable, "Name", new String[]{"IV", "DV"}, new String[]{"Apple", "Banana", "Canteloupe"}, new String[][]{new String[]{"Val1", "Val2", "Val3"}, new String[]{"D1", "D2", "D3"}});}, on this table:
Sym| Val1| D1| D2| Val2| Val3| D3 ----------+----------+--------------------+--------------------+----------+----------+-------------------- AAPL | 1| 7.7| 9.9| 3| 5| 11.11 SPY | 2| 8.8| 10.1| 4| 6| 12.12The expected output is:
Sym| Name| IV| DV ----------+----------+----------+-------------------- AAPL |Apple | 1| 7.7 AAPL |Banana | 3| 9.9 AAPL |Canteloupe| 5| 11.11 SPY |Apple | 2| 8.8 SPY |Banana | 4| 10.1 SPY |Canteloupe| 6| 12.12
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Table
columnsToRows
(Table source, String labelColumn, String[] valueColumns, String[] labels, String[][] transposeColumns) Convert value columns to labeled rows.static Table
columnsToRows
(Table source, String labelColumn, String valueColumn, String... transposeColumns) Convert value columns to labeled rows.static Table
columnsToRows
(Table source, String labelColumn, String valueColumn, String[] labels, String[] transposeColumns) Convert value columns to labeled rows.
-
Constructor Details
-
ColumnsToRowsTransform
public ColumnsToRowsTransform()
-
-
Method Details
-
columnsToRows
public static Table columnsToRows(Table source, String labelColumn, String valueColumn, String... transposeColumns) Convert value columns to labeled rows.- Parameters:
source
- the table with multiple value columnslabelColumn
- the output column name for the label columnvalueColumn
- the output column name for the value columntransposeColumns
- the names of the columns to transpose, the label value is the name of the column- Returns:
- the transformed table
-
columnsToRows
public static Table columnsToRows(Table source, String labelColumn, String valueColumn, String[] labels, String[] transposeColumns) Convert value columns to labeled rows.- Parameters:
source
- the table with multiple value columnslabelColumn
- the output column name for the label columnvalueColumn
- the output column name for the value columnlabels
- the labels for the transposed columns, must be parallel to transposeColumnstransposeColumns
- the input column names to transpose, must be parallel to labels- Returns:
- the transformed table
-
columnsToRows
public static Table columnsToRows(Table source, String labelColumn, String[] valueColumns, String[] labels, String[][] transposeColumns) Convert value columns to labeled rows.- Parameters:
source
- the table with multiple value columnslabelColumn
- the output column name for the label columnvalueColumns
- the output column names for the value columnslabels
- the labels for the transposed columns, must be parallel to each element of transposeColumnstransposeColumns
- an array parallel to valueColumns; each element is in turn an array of input column names that are constituents for the output column. The input columns within each element must be the same type, and the cardinality much match labels.- Returns:
- the transformed table
-