extend-datatable

Adds a new column to a datatable and optionally sets values in this new column based on another existing column.

Using a source column as a key, and defining a mapping of key-value pairs, creates a new column containing the values corresponding to the key in that row's source column.

Arguments

Inputs:

data: The datatable to which the new column will be added.

source column: The name of an existing column, which will be matched to the 'keys' in the values argument so that the 'values' can be used to fill in the new target column.

target column: (new column) The name of the new column to be added.

values: A map of key-value pairs. This mapping is used such that for a given value (key) in the source column, a corresponding value (value) should appear in the target column.

Output:

data: The datatable with a new column added.

Examples

Possible use cases

  • Add a column of cleaned data without losing access to the original values in the data source.

  • Add a column with a specific flag to identify rows where the source column has a certain value.

  • Add a column containing a new level of data (e.g. if you have city and state, add a column containing county, based on the city/state data)

Last updated