Calculate Index for Grid Questions
Jump to navigation
Jump to search
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript > Table JavaScript and Plot JavaScript Examples Library
This example may be used to compute an index for grid tables (Pick One - Multi and Pick Any - Grid) in the Blue drop-down menu when comparing to the last column which is generally the NET. As the rule borrows statistics from the table (by default it takes over the Missing n statistic), you should rename this statistic by right-clicking your table, selecting Table Options, and changing the name of Missing n to Index in the Override Text column of the Output Text column.
To use this snippet:
- Select your table.
- Select Automate > Custom Rule.
- Paste in the code from below.
- (Optional) change the statistic used in statistic_to_replace.
- Click the 'Play' icon and close.
- Right-click on your table and select Statistics - Cells > Missing n.
- Right-click your table, select Table Options, click Output Text, and change the name of Missing n to Index in the column of the Override Text column.
// Choose statistic to replace with calculated Index
var statistic_to_replace = "Missing n";
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.setSummary("Calculate index against last column");
See also
- 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 in the Variables and Questions tab using JavaScript.
Pages with syntax highlighting errors
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript > Table JavaScript and Plot JavaScript Examples Library