Create New Variables - Case-Level Shares
Jump to navigation
Jump to search
Displayr - Insert
Extensions
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Data Cleaning QScripts
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library
User Interface > Transformation
This tool computes shares from numeric data at the case (e.g., respondent) level. A simpler and often better method for computing shares is to use the share statistics available from Statistics - Cells on a table.
Example
Technical details
- Data with missing values will have shares of NaN for all variables.
- Where data contains negative values, negative shares will be computed.
- Generally the shares computed when showing the Average of the variables created using this QScript will differ to those obtained if selecting one of the share Statistics from Statistics - Cells, as the Average will show the average of the shares computed at the respondent (case level), whereas the shares computed using Statistics - Cells are created by summing up the data for each case and thus give greater weight to cases with larger values.
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
JavaScript
includeWeb("QScript Selection Functions");
includeWeb("QScript Utility Functions");// for areQuestionsValidAndNonEmpty
includeWeb("QScript Functions to Generate Outputs"); // for reportNewRQuestion
caseLevelShares()
function caseLevelShares() {
let question = selectInputQuestions(["Numeric - Multi"], single = true);
if (!question)
return false;
if (!areQuestionsValidAndNonEmpty(question))
return false;
let data_file = question.dataFile;
if (question != null){
let variables = question.variables;
// Computing the total
let total_var = preventDuplicateVariableName(data_file, "total");
let total = "var " + total_var + " = " + variables[0].name;
for (var j = 1; j < variables.length; j++)
total += " + " + variables[j].name;
total += ";\r\n" + total_var + " /= 100;\r\n";
let new_variables = [];
let last_var = null;
let prefix = makeid();
for (let j = 0; j < variables.length; j++) {
try {
let variable = variables[j];
let new_name = preventDuplicateVariableName(data_file, "shares" + prefix + "_" + (j + 1));
let expression = total + variable.name + " / " + total_var;
var new_var = data_file.newJavaScriptVariable(expression, false, new_name, variable.label, last_var, false);
} catch(e)
{
log("Could not create variable: " + e);
return false;
}
new_variables.push(new_var);
last_var = new_var;
}
// Set question and create table
let new_q_name = preventDuplicateQuestionName(data_file, question.name + " - Shares (%)");
let new_q = data_file.setQuestion(new_q_name, "Number - Multi", new_variables);
data_file.moveAfter(new_q.variables, question.variables[question.variables.length -1]);
insertAtHoverButtonIfShown(new_q);
reportNewRQuestion(new_q, "Shares");
return true;
}
}
See also
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.
Displayr - Insert
Extensions
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Data Cleaning QScripts
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library
User Interface > Transformation