How to set up Categorical Arrays using R
A Categorical Array is a Crunch-specific term that indicates a grid/matrix-style question, whereby a respondent can choose one option from a particular variable. Sometimes these are called a Matrix or even referred to as Grids (although different pick-any-row-or-column grids exist in questionnaires).
The following describes how you can make a Categorical Array using R.
The highlighted sections show you which values to change:
variables <- c("var1", "var2", var3") new_name <- "My New Array" ds$new_alias <- deriveArray(ds[,variables], name = new_name)
- Your dataset loaded and stored as ds
- A list of variables in a table (Excel or CSV), with its aliases (called variable names in SPSS) and labels you want to use
What to replace in the above template
- variables (var1, var2, var3)
- the list of variables that are will be used to create the array. They must all share the same categories (e.g., Agree to Disagree, or 0 to 10, or whatever range).
- My New Array
- the name you want to give the new array. You can use any name you want.
- the alias of your new array. You must specify one, which can be any made-up value as long as it hasn't been used previously. It should be a single string of characters and include no spaces (use underscores, periods, or dashes if you need to).
Creating a list of variables (a shortcut)
The following steps describe how to use an online tool to quickly create a long list of variables:
- Navigate to https://convert.town/column-to-comma-separated-list in a browser.
- Adjust the settings on the right-hand side (above the green box) to match the settings shown above (with settings for Delimeter, Item prefix, suffix, and so on.)
- Copy the resulting text into your R code (replacing the green text in the code template shown in the previous section).
The following example shows you how to use the auto-generated variables you created using the tool described above and then running it in your R app.
Suppose you have a dataset and a list (in Excel) of variables you want to delete:
- Dataset — toy dataset (Categorical Array).sav
- List of variables in Excel — variables for categorical array (Social News Post).xlsx
- Access your dataset.
- Use the 'shortcut' instructions shown above to quickly create a variable list.
- Follow the syntax shown in the code sample below to create a new R script.
- Run that code in your R app (R Studio).
- Refresh your web browser to check if it worked.
Your code should appear as follows:
# Create Derive array from "Social News Post" variables array_name <- "Social News Post" array_desc <- "Typically, how often do you POST (e.g., upload a photo, share a link,etc.) on the following social networks?" variables <- c("social_news_post_1", "social_news_post_2", "social_news_post_3", "social_news_post_4", "social_news_post_5") ds$social_news_post_array <- deriveArray(ds[,variables], name = array_name)
If you want to assign a variable name meaning, which is already available in the variable description, then run the following code where social_news_post_array is replaced with the desired variable:
# copy variable description to variable name names(subvariables(ds$social_news_post_array)) <- descriptions(subvariables(ds$social_news_post_array))