Create New Variables - Automatically Combine Categories - By Geography - United States - To Regions

From Q
Jump to navigation Jump to search

The features in Automatically Combine Categories > By Geography allow you to combine geographic data, such as postcodes or city names, into categories, such as states or regions. The features are organized in submenus based on region of the world and desired output categories. Entries that are not found in the regional database are assigned a value of "Other" in the created output variable.

This feature can be used to map an input variable containing United States ZIP codes, place names (e.g. cities and towns), counties, or states to a new variable containing the corresponding region: Northeast, West, Midwest, or South.

Example

The following table contains the raw values from an input variable containing US states in the left column. The right column contains the raw values of the output variable obtained from running this script with state selected.

Usage

  1. Select a table showing the question you want to use to create combined categories, or a single variable in the Variables and Questions tab.
  2. Select the region the data comes from and the desired output type from Automate > Browse Online Library > Automatically Combine Categories > By Geography.
  3. To change how the categories are combined:
    1. Select the new variable or question in the Variables and Questions tab.
    2. Right-click and select Edit R Variable.
    3. Choose the desired options in the Inputs section on the right.
    4. Click Update R Variable.
  1. Select a single variable under Data Sets containing geographic data. If the variable is numeric, then it should contain IP addresses or ZIP codes/postcodes.
  2. Click the + symbol to the right of the selected variable.
  3. Select the desired region then output type from Ready-Made New Variables > Automatically Combine Variables > By Geography.
  4. Choose options for the new variable in the Inputs section on the right of the screen.

Note: If some entries in your input data could refer to any of several possible locations with the same name in the region, you will be shown additional prompts where you can supply a supplementary variable (e.g. containing state/provinces names for each place) to disambiguate the entries to their correct location. In some instances, you may see this prompt when your input data is actually a different type, such as counties or states, in which case you can select the proper type using the prompts.

Options

Variable The input variable containing geographic data to be combined into categories.

Combine by Use this control to toggle between the other methods for combining categories in the the Automatically Combine Categories menu such as By Value > Tidy Categories.

World region The geographic region that the input data/variable comes from.

Input data type The type of data/geographic unit, such as States, Postcodes, or Place (city, town, etc.), that the input variable contains.

Output geographic type The desired geographic unit to combine the input data into. Must be a larger type than Input data type; e.g. it is possible to map U.S. counties to U.S. states, but not the other way around.

Check spelling If this option is selected then approximate matching is performed using the Levenshtein distance, instead of requiring exact matching when looking up the input data values in the regional database.

Check neighboring region Select this option if the input data comes from more than one region than the one specified by World region. For example, with World region set to USA and this option selected, matches for the input data will also be looked for within Canada.

Supplementary variable Only shown when Input data type is Place (city, town, etc.). Use this dropbox to supply an additional variable with geographic info (such as state or region) to disambiguate place names in the input data that could represent multiple distinct locations in the region.

How to apply this QScript

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Click on the QScript when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Select this QScript from the list.

Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

Customizing QScripts in Q4.11 and more recent versions

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
  • Press Edit a Copy (bottom-left corner of the preview).
  • Modify the JavaScript (see QScripts for more detail on this).
  • Either:
    • Run the QScript, by pressing the blue triangle button.
    • Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

Customizing QScripts in older versions

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.

JavaScript

includeWeb('QScript Functions for Geography');
includeWeb('QScript Functions for Automatically Combining Categories');
createAutomaticallyCombinedCategoryVariables('Geography', options = {region: 'USA', output_type: 'Region'});

See also