Introduction
This article describes how to go from having a numeric variable represented in aggregate:
To a state where you can assign a numeric variable into categories or bands (e.g., 0, 1-5, 6-10, 11+) for analysis purposes:
Requirements
Please note these steps require a Displayr license.
You will need to have a numeric variable. Numeric variables have a "2" next to them in the Data Sources tree:
OR
A scaled nominal or ordinal variable if text is involved with the value label (eg: 0 – Not at all likely and 10 – Extremely Likely are the endpoints of your scale). If using this type of variable, it is prudent to check the Values so that they align correctly with the labels. The Values button is just under the Structure dropdown in the object inspector as per the picture below. You don’t want a value of 1 ascribed to 0 - Not at all likely and so forth (it should be a value of 0). You should change it so that the correct value aligns with the label.
Method
- From the toolbar, go to Anything > Data > Variables > New > Custom Code > R - Numeric or hover between two variables in the data sources tree until a "+" appears, then select Custom Code > R - Numeric.
- In the R CODE window in the object inspector, paste in the following code:
##Identify variable
x = `Number of flights1`
##Set banded categories
x[x = 0] = 0
x[x >= 1 & x <= 5] = 1
x[x >= 6 & x <= 10] = 2
x[x > 10] = 3
##return all values
x
Note that the code above references the variable label ('number of flights1`) and it must be enclosed within backticks. You can also reference the variable name, which can be found by clicking on the variable in the Data Sources tree, then from the object inspector, go to General > GENERAL > Name. If I used the variable name instead, the code would look like this:
##Identify variable
x = q1_a_num
##Set banded categories
x[x = 0] = 0
x[x >= 1 & x <= 5] = 1
x[x >= 6 & x <= 10] = 2
x[x > 10] = 3
##return all values
x
You can augment and adjust the code to suit your banding needs, adding as many lines as you like.
4. Once you've made the variable you will need to adjust the Label for each Value, which you can do by going to the Value Attributes window using the Values button in the object inspector.
5. You can rename the new R variable from the object inspector by going to General > GENERAL > Name or you can right-click on the R variable in the Data Sources tree and select Rename.
6. In the object inspector, update the variable structure to nominal by going to Data > Properties > Structure > Nominal: Mutually exclusive categories.
See Also
How to Band Numeric Variables using a Table
How to Band Numeric Variables Using JavaScript
How to Create a New Variable Based on Other Variables using R
How to Work with Conditional R Formulas