In Memory Tables · Updating Tables · Practice Question
Rename a Table Column
Question
Given the following table:
t:([] sym:`A`B`C;
price:120.5 151.2 175.0;
time:12:00:00 12:01:00 12:02:00;
d:011b)Result:
sym price time d
--------------------
A 120.5 12:00:00 0
B 151.2 12:01:00 1
C 175 12:02:00 1Rename column d to priceCheck.
Solution
`sym`price`time`priceCheck xcol tResult:
sym price time priceCheck
-----------------------------
A 120.5 12:00:00 0
B 151.2 12:01:00 1
C 175 12:02:00 1Alternative Solution
([d:`priceCheck]) xcol tExplanation
The xcol operator renames table columns.
One approach is to provide the full list of column names:
`sym`price`time`priceCheck xcol tThis replaces all column names in order. Since the original columns are `sym`price`time`d, the fourth column, d, is renamed to priceCheck.
The alternative solution uses a rename dictionary:
([d:`priceCheck]) xcol tThe dictionary maps old column name → new column name, so only column d is renamed. Like the list form, it returns a new table; xcol does not accept a table name (`t), so to keep the result, assign it: t:([d:`priceCheck]) xcol t.
The full-column-list form is useful when renaming several columns at once or replacing all column names. The dictionary form is usually clearer when renaming only one or two columns.