Calculation - Average

From Q
Jump to navigation Jump to search
This page is currently under construction, or it refers to features which are under development and not yet available for use.
This page is under construction. Its contents are only visible to developers!

Averages each element for a single input or averages elementwise for multiple inputs This feature can be used to calculate the average (arithmetic mean) of Tables and R Outputs on the page or questionsvariable sets in Data Sets.

  1. If a single output on the page is selected, the average of all elements in the output is returned.
  2. If multiple outputs on the page are selected, a new output will be inserted containing the elementwise average. In the case of multiple inputs, the labels are inspected to match up the rows/columns of the inputs when averaging. This can be turned off using the Object Inspector of the created output. See the Inputs section below.
  3. If variables are selected when the feature is run from the menu, a new R Variable is created containing the average of the variables within case.

Usage

  1. To use this feature, first select either a) Variable Sets from Data Sets, or b) tables or R Outputs on the page that you wish to average.
  2. From the toolbar, select Calculation > Average or Anything > Calculation > Average.

If variables were selected in Step 1., the feature is also available by clicking the + sign when hovering in Data Sets and selecting Calculation > Average.

Example

Consider the following two tables of sales data.

After selecting the two tables and running this feature, the following output will be added to the page containing the elementwise average.

By default, the row for CAN is not included in the output because there is no matching entry in the second input table and the average is still computed for the AUS - 2018 cell even though there is missing data for one input. This can be changed using the settings in the Object Inspector when the output is selected.

Inputs

The outputh which shows the results of the calculation has the following options available in the Object Inspector.

Input The Q tables or R Outputs to be used in the calculation.

Calculate for inputs with incomplete data If this option is checked, than any missing values in any of the inputs will be ignored in the calculation. If unchecked, then missing values are not removed before calculation and will propagate as missing values in the output.

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 include This listbox shows all row labels in the inputs. The calculation will not performed for any rows whose labels are unselected here and they will not be shown in the output. By default, data reductions such as "SUM" and "NET" are not selected/shown in the output.

Columns to include As above, but for columns; any column labels unselected here will not be included in the output.

Technical Details

When there are multiple inputs, inputs that contain only a single row (column) may be recycled to a matrix/table with the same number of rows (columns) as the other inputs. For example, if the supplied inputs are a table with three rows and two columns and another table with two rows and a single column, the single column will be expanded by rows into a table with three rows and two columns with each row identical to the original column.

When Automatically match elements is set to Yes - show unmatched or Yes - hide unmatched, both exact matches and fuzzy matches (as described above) are considered, and the order of elements may be permuted so that the names match. It also may transpose an input if, for example, the column names of one input match the row names of another input.

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 Calculations");
includeWeb("QScript Functions to Generate Outputs");

if (!applyCalculationOnSelections("Average"))
    conditionallyEmptyLog("QScript cancelled.");

See also