This article describes how to create a set of dynamic variables for the current and previous waves based on the period selected in a control box:
Requirements
- A data set that includes a Nominal wave variable. In this example, we have a variable called Wave that tells us what quarter the survey was completed.
- A Control that includes the items from the wave variable. Here, we have data for Qtr 1 to Qtr 4. As we are interested in the current and previous waves, we have omitted Qtr 1 from our wave.combo.box control.
Method
1. Select your data set in the Data Sources tree.
2. Hover between any variables and click + > Custom Code > R > Numeric in the toolbar.
3. Paste the below into the R Code editor:
waves = Wave
unique.waves = levels(waves) #Get wave categories
curr.index = match(wave.combo.box,unique.waves) #Find index of current wave
waves %in% unique.waves[curr.index-1] #Return TRUE for previous wave
- The above code gets the levels or categories from the Wave variable and defines them as unique.waves.
- It then finds the index of the current wave selected in the control box within unique.waves. Note, this code assumes the waves are in the correct order.
- Finally, we return TRUE or FALSE based on whether the data matches the previous wave category.
4. Update Name and Label under General > General to Previous, for example.
5. Right-click the variable in the Data Sources tree and select Duplicate to copy this variable.
6. Replace the R code in the duplicated variable with the following:
Wave %in% wave.combo.box
This will simply return TRUE if the data in our Wave variable matches the category selected in the control box.
Alternatively, you can follow the steps outlined here to create a non-R based filter.
7. Update Name and Label under General > General to Current, for example.
8. Select both variables in the Data Sources tree, and right-click > Combine.
9. Update the variable set Label in the object inspector to Waves, for example.
10. Change Structure to Binary - Multi: Non-mutually exclusive categories.
Next
How to Work with R in Displayr