Sometimes you may have an option in a questionnaire which none of the respondents select, and if you are using an Excel-style file the category will not appear when you create a table for the question. You may wish to have a table which includes the missing category, and which shows 0%.
This article describes how to use QScript to add in an empty category to a question to go from this:
To this:
You can add this missing category in a few ways:
Method 1 - Using a new R variable
Requirements
- A Categorical or Ordered Categorical question without negative category Values. The examples below use the Gender variable from the Cola.Q example project. To access it, go to:
-
- Select File > Open > Example Project.
- Open Cola.Q
- Create a summary table using "Q2. Gender"
Method 1 - Using a new R variable
In this method, you create a new variable with the additional category using an R variable. You call the original variable and manually set the order of the categories using the factor() function. Note that if a new category comes in for the original variable that is not listed in the hardcoded list of categories, it will appear blank in the data until it is added.
- In the Variables and Questions tab, right-click and Insert Variable(s) > R Variable.
- Paste in the following code in the R CODE box on the right and edit to your needs:
#get original variable
orig=Gender
#make vector of all the categories you want available including missing in the order you want
thecats=c("Male","Female","Prefer not to answer")
#do the conversion into categories
a=factor(orig,levels=thecats) - Press the play button to run the code and give the variable a Question Name.
- Click Add R Variable. When the variable is added it will automatically convert to a Pick One question type.
Method 2 - Using back coding
If wanting to add a category into a question with a Pick One - Multi structure, see Additional details below. This method uses a fake text variable to back code into the categorical variable and use the categorization tool to add any missing categories. Note the values of the categories in the tool must match the values of the categories in the variable you are backcoding into.
- First, we'll create a fake text variable to back code. In the Variables and Questions tab, right-click and select Insert Variable(s) > JavaScript Formula > Text.
- Give it a Label like "dummy text".
- In the Expression box simply type
NaN
to create all missing values for the dummy text variable. - Click OK to create the new variable.
- With the dummy text variable selected, right-click and select Insert Variable(s) > New Code Frame > Manual Categorization > Mutually Exclusive Categories.
- The categorization tool will open. Right-click on New Category and Delete it.
- Click the Inputs and Back Coding button at the bottom.
- In the Corresponding back coding variables: field select the variable you'd like to add the category to in the dropdown, such as Gender in our example:
- Click OK and you will see the current categories added to the list on the right.
- Right-click and Add Category for the category you are missing:
- Repeat step 9 for as many categories as you are missing (note the number next to them in the list will be the underlying Value for that category in the new code frame).
- Click Save Categories.
- You will now have a new variable with the missing categories added:
Additional details
When adding a category a -Multi question using the back coding method, you cannot use a question with a - Multi structure directly in the process, so some additional steps are required. Before following the instructions above, first right-click one of the variables and select Split Variables from Question to split one of the variables from the -Multi question and use that variable in the back coding steps. Then afterward, select that split-off variable and the larger question, then right-click and use Set Question to add the back-coded variable to the larger question along with the new category.
Method 3 - Using a QScript
If you want to do this across many questions or simply add a category to a larger code frame, you can set up a larger QScript to do this.
1. Select your table.
2. Go to Automate > Open QScript (Macro) Editor.
3. Paste the below into the editor:
includeWeb("QScript Utility Functions");
includeWeb("QScript Value Attributes Functions");
var data_file = project.dataFiles[0];
var question = data_file.getQuestionByName("Q2. Gender");
var variable = question.variables[0];
// Create new copy of variable using JavaScript
var new_variable = data_file.newJavaScriptVariable(variable.name, false, preventDuplicateVariableName(data_file, variable.name), variable.label + " - Complete");
new_variable.question.questionType = "Pick One";
data_file.moveAfter([new_variable], variable);
// Copy existing values labels into new variable
copyValueAttributesForVariable(variable, new_variable);
// Add a new category by setting a label for a value that does not exist yet.
var value_attributes = new_variable.valueAttributes;
value_attributes.setLabel(3, "Prefer not to answer");
// Show the results
var new_table = project.report.appendTable();
new_table.primary = new_variable.question;
project.report.setSelectedRaw([new_table]);
4. Press the Play button.
5. The script then does the following:
- A new copy of the Gender question is created using a JavaScript variable.
- The original labels are copied into the new question.
- A new category is added to the new question by setting a label (Prefer not to answer) for a value which does not exist (3).
Next
How to Create a Custom QScript