Recode - Recode Variable(s) Using Code/Category Midpoints
This tool automatically recodes data according to numbers contained in the label of each category. If the labels contain a single number or numeric value then this will be used for the new value, and if the label contains a range of values then the mid-point between the two values will be used.
For generating new variables based on the same recoding, use the Create New Variables - Numeric Variable(s) from Code/Category Midpoints instead.
Usage
For Q:
- Select a table containing the data that you want to recode
- Select Automate > Browse Online Library > Recode > Recode Variable(s) Using Code/Category Midpoints
For Displayr:
- Select a table containing variable set you wish to recode, or select one or more variable sets under Data Sets
- Select + Anything > Data > Variables > Modify > Recode > Recode Variable(s) Using Code/Category Midpoints
Technical details
Where labels contain a single number, this value will be used. If no number is detected in the label, then the value of NaN will be assigned. Recoding will only be applied for questions that have three or more labels containing numbers.
Where the label contains a range of numbers, for example 18 to 24 then the midpoint value will be used (for example 21 in this case). If data is recoded according to mid-points and it contains a lower-bound label like Less than 18 then the midpoint will be half-way between zero and the number in the label (in this example 9). When data is recoded according to mid-points and it contains an upper label like 55 or more then the midpoint will be the number in the label plus half of the previous interval - so if the previous interval was 50 to 54 this midpoint will be set to 57. If no midpoint for a label can be determined then a value of NaN will be assigned.
If the labels include any kind of brackets, e.g. [[ or (, then only the text inside the brackets will be used. If there is no closing bracket (the label has been truncated) then everything after the opening bracket will be used.
Labels that contain references to time periods, such as days, weeks, minutes, and hours, or other units like litres or kilograms are difficult to recode in this way, because the scale of each data point can change based on the units. If you try to recode data with time periods or units, you will be prompted to check these.
Example
The first example shows the Value Attributes of a five-point scale of agreement. The data provider has coded the data so that the values for each category are reversed, and so computing an average of these values will not produce a meaningful answer. The data provider has instead provided the correct values in brackets in each label, i.e. 1. When this QScript is applied, the numbers from each label are used as the values. Any labels which do not contain numbers are assigned a value of NaN, which means that they will not contribute to the computation of any numeric statistics like Average or Sum.
The second example shows the Value Attributes of an Age question. The age categories have been given values (1 to 7) that do not relate to the age groups that are described by each label. Sometimes it can be helpful to recode a question like this so that the value of each category corresponds to the midpoint between the two numbers in each label in order to get an approximate average age. When this QScript is used it looks at the numbers in each label and recodes each category according to the midpoint.
How to apply this rule
For the first time in a project
- Select the table(s)/chart(s) that you wish to apply the rule to.
- Start typing the name of the Rule into the Search features and data box in the top right of the Q window.
- Click on the Rule when it appears in the QScripts and Rules section of the search results.
OR
- Select Automate > Browse Online Library.
- Choose this rule from the list.
Additional applications of the rule
- Select a table or chart that has the rule and any table(s)/chart(s) that you wish to apply the rule to.
- Click on the Rules tab (bottom-left of the table/chart).
- Select the rule that you wish to apply.
- Click on the Apply drop-down and choose your desired option.
- Check New items to have it automatically applied to new items that you create. Use Edit > Project Options > Save as Template to create a new project template that automatically uses this rule.
Removing the rule
- Select the table(s)/chart(s) that you wish to remove the rule from.
- Press the Rules tab (bottom-right corner).
- Press Apply next to the rule you wish to remove and choose the appropriate option.
How to modify the rule
- Click on the Rules tab (bottom-left of the table/chart).
- Select the rule that you wish to modify.
- Click Edit Rule and make the desired changes. Alternatively, you can use the JavaScript below to make your own rule (see Customizing Rules).
JavaScript
//Recoding - Midpoint Coding and Quantification
includeWeb('QScript Questionnaire Functions');
includeWeb('QScript Functions to Generate Outputs');
midpointAndQuantificationRecoding(false);
See also
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Data Cleaning QScripts
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library