When analyzing numeric data (i.e. Age in years or Income in $), you may want to create categories from these value 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 (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 can be found in the Data tab of the object inspector 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
- From the toolbar, go to Anything > Data > Variables > New > Custom Code > JavaScript - Numeric or hover between variables in the data sources tree until a "+" sign appears and select Custom Code > JavaScript - Numeric.
- In the General > Javascript Code window in the object inspector, 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 button in the Data tab of the object inspector.
5. You can rename the new JavaScript variable from the object inspector by going to General > Name or you can right-click on the JavaScript variable in the Data Sources tree and select Rename.
6. Update the variable structure to nominal by going to General > Structure > Nominal: Mutually exclusive categories from the object inspector.
Next
How to Band Numeric Variables Using a Table