This example may be used to compute an index for grid tables (Nominal - Multi and Nominal - Grid) in the Rows of a table when comparing to the last column which is generally the NET.
Technical details
This rule replaces the Missing Count statistic with the custom index calculations and renames it Index. If wanting to show the actual Missing Count on the table as well, you can pick a different statistic to override by modifying the code as described below.
Method
- Select your table.
- Add the statistic in Statistics > Cells that you'd like to overwrite with your Index values (it is Missing Count by default).
- Create a custom rule:
- In Q: Select Automate > Custom Rule.
- In Displayr: Select Properties > RULES > + > New Custom Rule and click Edit JavaScript.
- Paste in the code from below.
- Optional: change the statistic used for statistic_to_replace.
- Optional: change the statistic used to calculate the index in line 5 from "%" to "Column %" or whatever is appropriate.
- Click the 'Play' icon and OK/close.
// Choose statistic to replace with calculated Index
var statistic_to_replace = "Missing Count";
var idx = table.get(statistic_to_replace);
var pct = table.get("%");
// Choose last column to compare against, i.e. NET
var idx_col = table.numberColumns - 1
// Loop through each column and row
for (col = 0; col < table.numberColumns; col++)
{
for (row = 0; row < table.numberRows; row++)
{
idx[row][col] = (pct[row][col] / pct[row][idx_col]) * 100
}
}
// Replace with Index value
table.set(statistic_to_replace, idx);
form.setTranslation(statistic_to_replace, "Index");
form.setSummary("Calculate index against last column");
Next
- Table JavaScript and Plot JavaScript for an explanation of how to run this code.
- Table JavaScript and Plot JavaScript Reference for technical information.
- Table JavaScript and Plot JavaScript Examples Library for other examples.
- JavaScript for information about the JavaScript programming language.
- QScript for tools for automating projects using JavaScript.
- JavaScript Variables for detail on how to create new variables using JavaScript.