Back to Updating Tables

In Memory Tables · Updating Tables · Practice Question

Rename a Table Column

Sign in to track your progress

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 1

Rename column d to priceCheck.

Solution

`sym`price`time`priceCheck xcol t

Result:

sym price time     priceCheck
-----------------------------
A   120.5 12:00:00 0
B   151.2 12:01:00 1
C   175   12:02:00 1

Alternative Solution

([d:`priceCheck]) xcol t

Explanation

The xcol operator renames table columns.

One approach is to provide the full list of column names:

`sym`price`time`priceCheck xcol t

This 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 t

The 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.