Create New Variables - Automatically Combine Categories - By Geography - United States - To Regions
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
- Select a table showing the question you want to use to create combined categories, or a single variable in the Variables and Questions tab.
- Select the region the data comes from and the desired output type from Automate > Browse Online Library > Automatically Combine Categories > By Geography.
- To change how the categories are combined:
- Select the new variable or question in the Variables and Questions tab.
- Right-click and select Edit R Variable.
- Choose the desired options in the Inputs section on the right.
- Click Update R Variable.
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
JavaScript
includeWeb('QScript Functions for Geography');
includeWeb('QScript Functions for Automatically Combining Categories');
createAutomaticallyCombinedCategoryVariables('Geography', options = {region: 'USA', output_type: 'Region'});
See also
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library