Map

Define key-value pairs

The Map data type represents a collection of key-value pairs. It is a way to associate one piece of information with another, similar to "recode" function in R or SQL. It is like a two-column list.

A key-value pair consists of two related data elements, and it can be used a bit like an “if this, then that” statement (“if this key, then that value”).

Within a map, the Key must be unique, but the Value doesn't need to be unique. The Key is the unique identifier, and the Value is the information associated with the Key. Each Key is only associated with one Value, but the same Value can be assigned to multiple Keys.

Examples

Extend-datatable to set colours

The Extend-datatable adaptor accepts a Map for the values argument. This argument should provide mapping such that for a given value in the `source` column, a corresponding value should appear in the target column.

If you have a spreadsheet containing a two-column set of column, column__colour then you can pull that into Data-flo and use extend-datatable to associate those colours with the rest of your data.

Harmonise data values

Again using Extend-datatable, we can harmonise inconsistent spellings using a data map.

TIP: The map must include all desired key-value pairs. Don't forget to include the map of values that are not changing (e.g. not just "uk | UK" but also "UK | UK".

Showing map in JSON format

Given the following Datatable:

datatable.json
{
  "columns": [
    "city"
  ],
  "rows": [
    { "city": "Manchester" },
    { "city": "London" }
  ]
}

This map would assign a colour to each city:

map.json
[
  [ "Manchester", "red" ],
  [ "London", "blue" ]
]

Resulting in this Datatable:

extended-datatable.json
{
  "columns": [
    "city",
    "colour"
  ],
  "rows": [
    { "city": "Manchester", "colour": "red" },
    { "city": "London", "colour": "blue" }
  ]
}

Tips

TIP: For a map that may need frequent editing or extension, you can create a dictionary/library of the mapped terms in an external file (csv, spreadsheet, etc).

TIP: If mapping based on an existing column (e.g. in Extend-datatable), use datatable-to-list and unique-list-items to identify all the source column values and ensure that you include all necessary key-value pairs.

Last updated