This article describes how to go from two single-response variables of mutually exclusive data...
...to a single variable of merged data:
This scenario can occur when you have different segments that were asked a different set of survey questions.
Requirements
- Two or more single Nominal variables with mutually exclusive data.
Please note these steps require a Displayr license.
Method
1. In the Data Sources tree, hover over any variable and click the Plus (+) then select Custom Code > R - Numeric.
2. Update the Name and Label on the object inspector under the General tab.
3. In the Data tab, paste the below under R Code - Data:
library(tidyr)
# join variables together in same set
df = data.frame(q1_1,q1_2)
# create merged column
m = df %>% unite("Merged", na.rm = TRUE, remove = FALSE)
m = m[,1]
# combine code frame
labels = unique(m)
# assign code frame
merged = factor(m, levels = labels)
- The above code groups the input variables together. In this example, the variables are referenced via variable name which is found in the object inspector under General > Name.
- We then use tidyr's unite function to merge the data into a single column.
- Using the unique function, we store all the unique value labels from both variables. This ensures if there are any differences, such as in this example with Refuse to answer and Don't care, that they both get recorded correctly.
- Finally, we set the unique labels as the categories in the new merged variable.
4. Click Calculate.
5. Change Data > Properties > Structure to Nominal: Mutually exclusive categories.