Calculation - Divide
This tool divides the numbers in one table or output by those in another. You may also choose either the denominator or numerator of the division to be a single number. If variables are selected, then new variable(s) will be created by dividing values the first set by values in the second for each case.
Usage
Divide One Table by Another
To divide the values in one table by those in another:
- Select the table you wish to divide (the numerator).
- Hold CTRL on your keyboard.
- Select the table you wish to divide by (the denominator).
- Use Automate > Browse Online Library > Calculation > Divide
- Change the Options to determine how to match rows and columns between the two tables, and choose which rows and columns are included in the division (see below).
Divide One Variable by Another
To create a new variable which contains the result of dividing the values from one variable by those of a second variable for each case in the data set:
- Select the variable you wish to divide (the numerator) in the Variables and Questions tab .
- Hold CTRL on your keyboard.
- Select the variable you wish to divide by (the denominator).
- Use Automate > Browse Online Library > Calculation > Divide
Divide One Question by Another
You can also divide one question by another when those two question s have variables which match. For example, you may have two question that both contain scores for the same set of brands, and you wish to divide the two scores for each brand to create a new metric. In this case, a new question is created with one new variable for each matching item, and the values are divided for each case in the data set.
To do so:
- Select the question you wish to divide (the numerator) in the Variables and Questions tab .
- Hold CTRL on your keyboard.
- Select the question you wish to divide by (the denominator).
- Use Automate > Browse Online Library > Calculation > Divide
Note: It is not possible to update the new variables when the matching between the two input question s changes (for example if you merge the scores for two brands together). If you change the number of items in one or both of the question s you should repeat the steps above to obtain new divided values.
Examples
The table on the left shows the numbers of men and women in the sample who said they ate at various restaurants in the previous month, and the table on the right shows the numbers of men and women in the sample who said they have ever eaten at those restaurants.
Using Divide produces a new table which contains the proportion of men and women who ever eat at each restaurant who also ate at each restaurant in the past month.
Note that the NET rows and columns have been excluded by default. You can choose whether or not to include these by changing the options.
Options
Divide the Choose whether you wish the numerator of the division to be an output containing one or more numbers, or a single value. When Output is selected, you can choose an output in your document. When Single numeric value is selected you can then type in the desired value.
by the Choose whether you wish the deniminator of the division to be an output containing one or more numbers, or a single value. When Output is selected, you can choose an output in your document. When Single numeric value is selected you can then type in the desired value.
Automatically match elements Only shown when there are multiple inputs to Input. This controls how and whether matching is done between the labels of the inputs. The default, "Yes - hide unmatched", will look for matching labels in the rows and columns of the inputs before proceeding with the calculation, and any rows/columns that are not contained in all the inputs will not be included in the output. See the Example. For a full description of the matching algorithm, see the Technical Details. "Yes - show unmatched" will also perform matching, but any unmatched rows (columns) will appear in the output as rows (columns) of all missing values. Selecting "No" for this option will cause any labels in the data to be ignored and not perform any matching. Selecting "Custom" will bring up two additional controls that allow for specifying the matching behavior for rows and columns separately.
Match rows Only shown if Automatically match elements is set to "Custom". Specifies the matching behavior when comparing row labels of the inputs. "Yes - show unmatched" and "Yes - hide unmatched" look for exact matches in the row labels in the inputs. "Fuzzy - show unmatched" and "Fuzzy - hide unmatched" perform fuzzy matching so that labels that differ only by a single character are considered to be a match.
Match columns Only shown if Automatically match elements is set to "Custom". The options are the same as Match rows, but control the matching between columns.
Rows to exclude Here you can type in row labels that should be excluded from the calculation.
Columns to exclude As above, but for columns.
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 Calculations');
includeWeb('QScript Functions to Generate Outputs');
applyCalculationOnSelections('Divide');
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.
Calculation
Q Technical Reference
Q Technical Reference
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