When analyzing numeric data (i.e. Age in years or Income in $), you may want to create categories from these values to analyze vs using the numeric values themselves. You can achieve this in Displayr using simple JavaScript code (though there are other methods available too). This article describes how to use a numeric variable, shown as an average in aggregate:
to create banded categories (e.g., 0, 1-5, 6-10, 11+) for analysis purposes using JavaScript:
Requirements
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 there is text involved with the value label (e.g., 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 & Labels button can be found in the Data > Attributes tab of Properties when a variable or variable set is selected in the Data Sources tree, 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
- Hover over a variable in the Data Sources tree and click + > Custom Code > JavaScript > Nominal.
- In the code
panel, paste in the following code:
//Identify variable name
x = q1_a_num
//Create banded categories
if (x == 0) 1;
else if (x >= 1 && x <= 5) 2;
else if (x >= 6 && x <= 10) 3;
else if (x > 10) 4;
In the statement above, q1_a_num is the numeric variable that I would like to create new bands for. You can augment and adjust the code to suit your banding needs, adding as many lines as you like.
4. Click Calculate.
5. 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 & Labels button in the Data > Attributes of Properties .
5. You can rename the new JavaScript variable from Properties by going to General > General > Name or right-clicking the JavaScript variable in the Data Sources tree and selecting Rename.
Next
How to Band Numeric Variables Using a Table