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. That is, if your variable is a scaled question 1-5 and no respondents selected 1, the Values for the variable in Displayr would be 2-5 and not show the 1 category. However, you may wish to have a table that includes the missing category with 0%.
You can add this missing category in 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
- 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 Sources tree, hover over a variable and click + > Custom Code > R - Numeric.
- Paste in the following code in the R Code editor 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
This method back codes a dummy text variable into the original categorical variable and uses the Text Categorization module to add any missing categories. A couple ofthings to keep in mind:
- The values of the categories in the categorization tool must match the values of the categories in the original variable you are back coding into. That is, the numbers next to the category in the tool must be what they were in the original variable.
- If you update the data later on and the missing option appears,
- If wanting to add a category into a variable set with a Binary - Multi or Nominal - Multi structure, see Additional details below.
To use this method:
- First, we'll create a fake text variable to use in the Text Categorization module. In the Data Sources tree, hover and click + > Custom Code > JavaScript - Text.
- Give it a Label like dummy text.
- In the JavaScript Code editor box simply type
NaN
to create all missing values for the dummy text variable. - Click Calculate.
- With the dummy text variable selected, hover over the tree and select + > Text Categorization.
- Select Only one theme and click Start.
- The categorization tool will open.
- 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 gender categories added to the themes list on the left.
- Right-click and Add Theme.
- Give the new theme a name for the gender category you are missing and click OK.
- Repeat Steps 11-12 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.
- You will now have a new variable with the missing categories added:
Additional details
When adding a category to 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 More tools
> your company name > Open QScript 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.
5. Click OK.
Your new "Gender - Complete" variable will now be found in the Data Sources tree.
Next
How to Modify Variables and Value Attributes via QScript
How to Add a Category for the Value Attributes Using the Displayr API