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 few ways:
Method 1 - Using a new R variable
Any 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):
Requirements
Please note these steps require a Displayr license.
- An Ordinal or Nominal variable set without negative category Values. This example uses the Gender variable from the cola example data set.
- For Method 3, 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
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)
#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 Binary - Multi or Nominal - 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 Data Sets tree, hover and click + > Custom Code > JavaScript - Text.
- Give it a Label like dummy text.
- In the JAVASCRIPT CODE box simply type
NaN
to 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:
Additional details
When adding a category a -Multi variable set using the back coding method, you cannot use a variable set with a - Multi structure directly in the process, so some additional steps are required. Before following the instructions above, first Split off one of the variables from the -Multi variable set and use that variable in the back coding steps. Then afterward, select that split-off variable and the larger variable set, then use Combine to add the back coded variable to the larger variable set along with the new category.
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[0];
////EDIT Gender with your variable set label
var question = data_file.getQuestionByName("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;
////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;
project.report.setSelectedRaw([new_table]);
4. Press the Play button.
Your new "Gender - Complete" variable will now be found in the Data Sets tree.
Next
How to Modify Variables and Value Attributes via QScript
How to Add a Category for the Value Attributes Using the Displayr API