QScript DataReduction

From Q
Jump to navigation Jump to search

DataReduction

The data reduction is the list of codes that appear as row/column headings in tables. You can perform the same operations here as you can in Q: merge, copy, hide, etc. The functions in this class take strings to specify rows or columns using their labels, but you can also specify rows/columns using numbers. For one-dimensional questions, use positive integers starting from zero. For Pick One - Multi questions, positive integers indicate columns (values) and negative integers indicate rows (variables). For Grid questions positive integers indicate rows and negative integers indicate columns. Use the JavaScript ~ operator to turn a positive number, starting at zero, negative. Do not use the minus sign. Note that this indexing is not affected by the question being transposed.

clearSpans()

Removes all spans. Spans are an extra level of labels that can cover multiple rows or columns.

columnLabels

Returns an array of column labels (strings) for this data reduction. The will return null if this question does not show columns in a SUMMARY table.

columnSpans

As per ModifyTableOutput.columnSpans.

contains(code)

Determine whether code exists in this data reduction.
codeThe label of the code. e.g. 'Pepsi'
Returns:row or column or null
Example:
if (age_q.dataReduction.contains('15 and under')) log('found it!')

copyAfter(code, after_code)

Copies a code to another position in the data reduction.
codeThe label of the code. e.g. 'Pepsi'
after_codeThe label of the code after which the copy should be placed. null to move it to the beginning.

createNET(codes, name)

Creates a new code by merging multiple codes, leaving the original codes in the data reduction, as for the the popup menu with the same name.
codesAn array of code labels. e.g. ['Pepsi', 'Diet Pepsi']
nameThe name for the new, merged, code. e.g. 'Pepsi NET'

equals(obj)

Whether two objects are the same.
objThe object to compare against.
Returns:true or false
Example:
data_file.getQuestionByName('Q2').equals(data_file.questions()[0])

getUnderlyingValues(code)

Tells you which numeric response value underlies a code. If a code has been created by merging responses (or if it is a NET) then multiple values will be returned. These values can looked up in the question's ValueAttributes object. This only applies to Pick One, Pick One - Multi and Pick Any - Compact questions.
codeThe label of the code. e.g. 'Pepsi'
Returns:An array of numbers, each of which is the source (original) variable value.
Example:
var values = q.dataReduction.getUnderlyingValues('Pepsi');
Array.forEach(values, function (v) { log(v); });

getUnderlyingVariables(code)

Tells you which variable underlies a code. If a code has been created by merging codes (or if it is a NET) then multiple variables will be returned. This only applies to Pick Any, Pick One - Multi and Number - Multi questions.
codeThe label of the code. e.g. 'Pepsi'
Returns:An array of variables.
Example:
var vars = q.dataReduction.getUnderlyingVariables('Pepsi');
Array.forEach(vars, function (v) { log(v.name); });

hiddenValues()

Returns all values hidden for this question. The label for each returned object can be accessed with originalValueLabel.

hide(code)

Hides a code, as for the the popup menu with the same name. If you want to remove a code altogether then mark its values missing using question.valueAttributes.
codeThe label of the code. e.g. 'Pepsi'

merge(codes, name)

Merges multiple codes into a single code, removing the original codes, as for the the popup menu with the same name.
codesAn array of code labels. e.g. ['Pepsi', 'Diet Pepsi']
nameThe name for the new, merged, code. e.g. 'Pepsi'

moveAfter(code, after_code)

Moves a code to a new position in the data reduction.
codeThe label of the code. e.g. 'Pepsi'
after_codeThe label of the code after which it should be placed. null to move it to the beginning.

netColumns

Gets an array of indices corresponding to NET or SUM columns.

netRows

Gets an array of indices corresponding to NET or SUM rows.

rename(code, new_name)

Renames a data reduction code.
codeThe label of the code.
new_nameThe new label.
Example:
q.dataReduction.rename('Coke', 'Coca Cola');

revert()

Reverses any changes to the DataReduction that have been made since its Question was created. Any hidden codes will reappear, merged codes will unmerge, etc.

rowLabels

Returns an array of row labels (strings) for this data reduction.

rowSpans

As per ModifyTableOutput.rowSpans.

span(codes, label)

Span a number of rows/columns with a new label. This adds an extra level of labels that covers the normal labels.
codesAn array of code labels to span.
labelThe new label which will span over the normal labels given.
Example:
 // Span the Coke brands.
 q.dataReduction.span(['Coca Cola', 'Diet Coke', 'Coke Zero'],
                      'Coke Brands');

 // Span the Pepsi brands.
 q.dataReduction.span(['Pepsi Light', 'Pepsi Max', 'Pepsi'],
                      'Pepsi Brands');

transposed

Whether rows and columns have been swapped for this question. Only makes sense for questions with two dimensions (Grid and Pick One - Multi questions). Pick One - Multi questions are transposed by default.
Returns:Returns true if this question has been transposed.
Example:
q.transposed = !q.transposed;  // transpose q

type

Returns 'DataReduction'.

unhide(value)

Un-hide the given category from the DataReduction, where it was previously hidden by hide. It will be restored as a Code with a lone value.
valueThe object to unhide.