Calculation - Subtract
This tool subtacts the numbers in one table or output from those in another. You may also choose to subtract a single number, or to subtract from a single number. If variables are selected, new variable(s) are added to the data set which contain the subtracted values for each case.
Usage
Subtract One Table from Another
To subtract the values in one table from those in another:
- Select the table you wish to subtract from.
- Hold CTRL on your keyboard.
- Select the table you wish to subtract.
- Use Automate > Browse Online Library > Calculation > Subtract
- 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 subtraction (see below).
Subtract One Variable from Another
To create a new variable which contains the result of subtracting the values of one variable from those of another for each case in the data set:
- Select the variable you wish to subtract from, in the Variables and Questions tab .
- Hold CTRL on your keyboard.
- Select the variable whose values are to be subtracted.
- Use Automate > Browse Online Library > Calculation > Subtract
Subtract One Question from Another
You can also subtract one question from another when those two question s have variables which match. For example, you may have two sets of variables that both contain scores for the same set of brands, and you wish to compute the difference of the scores for each brand. In this case, a new question is created with one new variable for each matching item, and the values are subtracted for each case in the data set.
To do so:
- Select the question you wish to subtract from, in the Variables and Questions tab .
- Hold CTRL on your keyboard.
- Select the question whose values are to be subtracted.
- Use Automate > Browse Online Library > Calculation > Subtract
Note: It is not possible to update the new variables when the matching between the two input variable sets 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 subtracted values.
Examples
The table on the left shows the awareness results for a selection of restaurtants from September 2017, and the table on the right shows the awareness results from August 2017.
Using Subtract produces a new table which contains month-on-month differences for awareness:
Note that the NET rows have been excluded by default. You can choose whether or not to include these by changing the options.
Options
From the Choose whether you wish the first term in the subtraction 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.
subtract the Choose whether you wish the second term in the subtraction 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('Subtract');
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