# Calculation - Count - Count

Jump to navigation Jump to search

Counts the number of elements in a single input that satisfy a user specified condition, calculates the count element-wise for multiple inputs

This tool is used to count the number of cells in a table whose values match certain conditions:

• Are greater than, or less than, a certain number.
• Are greater than or equal to, or less than or equal to, a certain number.
• Are within a specified range of numbers.
• Are missing values (NA).
• Are identical to a specific category label.
• Satisfy a combination of conditions or ranges such as those listed above.

If variables are selected then this tool will create a new variable that counts the number of matches for each case in the data set.

## Usage

### Count Cells in a Single Table

To count the number of cells in a table that match a given condition:

1. Select the table.
2. Select Automate > Browse Online Library > Calculation > Count > Count.
3. Change the Values to count to the set of conditions you wish to count (see below).

### Count Cells between Two or More Tables

If you have two or more tables then Count will match the rows and columns of the tables and produce a new table which tells you for how many of the input tables each cell matches the specified condition:

1. Select the tables.
2. Select Automate > Browse Online Library > Calculation > Count > Count.
3. Change the Values to count to the set of conditions you wish to count (see below).

### Count Values Among Variables

You can use Count to count the number of times a set of variables match a condition, for each case in the data set. For example, you may have a set of rating scales for different brands, and you want to know how many times each case gave a score of 5 or more. To do so:

1. Select two or more variables under Data Sets.
2. Select Automate > Browse Online Library > Calculation > Count > Count.
3. Change the Values to count to the set of conditions you wish to count (see below).

## Examples

If you apply Count to a single table, then the result will be a single number which tells you how many cells in the table satisfy the given condition. For example, if we apply it to this table

and ask to count values >90, then the result is

Note that NET rows and columns are excluded by default, so the last cell in the table is not included (which would instead result in the answer of 5). You can choose which rows and columns are considered in the count by changing the options (see below).

When you have two or more tables as inputs, then Count will match the cells based on common row and column labels, and it will tell you how many of the tables have a matching cell that satisfies the condition. For example, if we use these two tables in count, which show Awareness for two different months,

and ask to count values >=70, then the result will be a new table like this

The result has matched up the brands by label, and the figures in the new table count how many of the input tables have values >=70 for each match.

## Options

Input The tables to be used in the calculation.

Values to count Enter ranges, single values, or inequalities to include in this calculation. Allowed options include:

• Single values, like 10 - include values exactly equal to a single value, e.g. values which are equal to 10.
• Sequences of values, like 1, 2, 3.5 - include values that are exactly equal to any of the entered values.
• Ranges of values, like [1-10] - include all values that fall within a given range. For example, the range listed to the left will match values between 1 and 10, inclusive.
• Inequalities, like >5.3 - include all values which satisify the inequality For example, the inequality to the left will match all values greater than 5.3. This works for greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=).
• Missing values, NA - match all values which are missing.
• Infinities, Inf, -Inf - include all values which are infinity or negative infinity.

Categorical Labels When one or more of your inputs is a variable with categories, you can type in the labels of the categories that you wish to match.

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 exclude Here you can type in row labels that should be excluded from the calculation.

Columns to exclude As above, but for columns.

## 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');

applyCalculationOnSelections('Count');