Sometimes you may have an option in a questionnaire which none of the respondents select. If you are using an Excel-style file or an SPSS file with incomplete code frames, 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%. You can add this missing category a couple of ways:
Both will take you from a variable set with incomplete categories (shown in a table):
to one with the missing category included (Prefer not to answer below):
- A Ordinal or Nominal variable set without negative category Values. This example uses the Gender variable from the cola example data set.
- For Method 2, you must have an Enterprise Displayr license and be familiar with How to Use QScripts in Displayr.
Method 1 - Using a new R variable
This is the most robust method for creating a new variable with the additional code. 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 Data Sets tree, hover over a variable and click + > Custom Code > R - Numeric.
- Paste in the following code in the R CODE box in the object inspector and edit to your needs:
#get original variable
#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
#click Calculate while Structure is numeric and then change to Nominal
- Click Calculate so the values are calculated when the variable is numeric.
- Then change Structure > Nominal to create the categories.
Method 2 - Using back coding
If wanting to add a category into a variable set with a - 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.
- Give it a Label like dummy text.
NaNto create all missing values for the dummy text variable.
- With the dummy text variable selected, hover over the tree and select + > Text Categorization > Manual > Mutually Exclusive Categories > New.
- 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:
Method 3 - Using a QScript
This method uses a QScript to add the value specified.
- Open the document with your data.
- Open the QScript editor through Anything > your company name > Open QScript Editor.
- Go to Automate > Open QScript (Macro) Editor.
- Paste the below into the editor and look for ////EDIT for where to change the question name and add your new values:
includeWeb("QScript Utility Functions");
includeWeb("QScript Value Attributes Functions");
var data_file = project.dataFiles;
////EDIT Gender with your variable set label
var question = data_file.getQuestionByName("Gender");
var variable = question.variables;
new_variable.question.questionType = "Pick One";
// Copy existing values labels into new variable
// Add a new category by setting a label for a value that does not exist yet.
var value_attributes = new_variable.valueAttributes;
////EDIT change 3 to a new value for the new category
////change Prefer not to answer to your new category label
////to add more categories add additional copies of the line below
value_attributes.setLabel(3, "Prefer not to answer");
//OPTIONAL Show the results in a table
var new_table = project.report.appendTable();
new_table.primary = new_variable.question;
4. Press the Play button.
Your new "Gender - Complete" variable will now be found in the Data Sets tree.
Regarding adding a category a -Multi variable set in Method 1, you cannot use a - Multi structure as the variable set to back code into so some additional steps are required. Before the instructions above, you'll need to Split off one of the variables from the -Multi set to use in the back coding. Then afterwards, Combine the -Complete variable created back into the -Multi set.