In some cases, it is useful to create a variable set that represents all combinations of the categories/responses from two or more variables. For example, you may ask yourself, "How can I merge my Age and Gender variables so I have categories from both?" or "How can I see what combinations of products are chosen together?" There are two possible methods depending on how your variables are structured. The Convert to > Binary-Multi > All Combinations works across multiple variable sets while you can create a custom R variable to show all combinations of items selected in a Binary-Multi variable set.
For example, using Convert to > Binary-Multi > All Combinations will create a variable set that represents all combinations of age and gender. You will start with variable sets like this:
and permutate all of the categories from each into new categories in a single variable set showing all combinations:
Creating a variable set in this way is similar to creating a banner, but allows more flexibility when manipulating the resulting variable set. This method is described in Method 1 - All Combinations across multiple variable sets.
Alternatively, you may want to create a new variable set that shows all combinations of a single binary - multi (Pick Any) variable set. For example, I have data about bubble gum flavors that people like, and respondents were able to select more than one flavor:
I want to create a new variable that includes all of the combinations that people selected and display it in a table to see which combinations are most popular:
See Method 2 - All combinations across a single binary - multi variable set for steps to set up a custom variable using R code to create a variable that summarizes all combinations of a binary - multi variable set.
Note that if you are looking to combine the same groups of categories in multiple variable sets at once, like grouping Net Promoter Scores across multiple variables, you should combine categories across multiple variable sets instead.
Requirements
A Displayr document with a data set including at least one variable set. Note that these variable sets cannot be text variables.
Method 1 - All Combinations across multiple variable sets
- In the Data Sources tree, select the variable set(s) that you want to use to create the variable set of all combinations.
- Hover over one of the variable sets and click + > Convert To > Binary - Multi > All Combinations.
A new binary - multi variable set will be created that you can use in other analyses, including creating a filter. See Additional Notes for more information.
Method 2 - All combinations across a single binary - multi variable set
- In the Data Sources tree, hover and click + > Custom Code > R > Nominal.
-
In the R code editor, paste the following code:
#select Binary-Multi set values will be 1 and 0 thevar=Flavors #remove net thevar = thevar[,colnames(thevar) != "NET"] #pull off option names thenames=colnames(thevar) #combine selections makecombination <- function(x){ paste(thenames[which(x == 1)],collapse=", ") } selectedcombinations = apply(thevar, 1, makecombination) #create list of all combinations of the variable labels allcombinations = unlist(sapply(1:length(thenames),function(n) apply(combn(thenames, n),2,function(x) paste(x,collapse=", ")))) #create result where code frame includes all combinations of the variables - even if not present in respondents selectedcombinations=factor(selectedcombinations, levels = allcombinations) #if you only want the code frame to include combinations from the data, comment out the line above and comment in the line below # selectedcombinations=factor(selectedcombinations, levels = unique(selectedcombinations))Note that you will need to update Line 2 of the code, replacing Flavors with the binary - multi variable set's Label. You can use the point and click method to replace Flavors with your variable set's label, see How to Use Point and Click Inside R Code.
Additionally, you can comment out Line 19 of the code and comment in Line 21 if you want to show only the selected combinations. This will remove any non-selected combinations from the final result.
- Click Calculate in the object inspector.
- In the General tab of the object inspector, give this new variable a Name and Label.
You can now use the variable in other analyses, including creating a summary table by dragging it onto a page or into the Report tree.
Additional Notes
If using Method 1 above, the resulting variable set has a Binary - Multi structure. This feature is often useful even if the goal is to create a unique filter variable, say, Women aged 34-45. Creating an All Combinations variable set will produce all the combinations of age and gender, which can then be tagged Usable as a Filter. This allows you to use each of the combinations as independent filter variables.
Additionally, you can use + > Convert To > Nominal to change the resulting binary - multi variable set into a nominal variable set. See How to Convert Variables with Mutually Exclusive Data to a Single Variable for more information.
Next
How to Convert Variables with Mutually Exclusive Data to a Single Variable
UPCOMING WEBINAR: 10 Market Research Predictions Over the Next 4 Years