Although Displayr automates the creation of the most commonly required variables, sometimes there's a need to create a new variable using custom code. Usually, the best way to do this is to create a custom R variable. You can also create multiple R variables if needed, see Create multiple custom variables below. This article covers using a custom R variable to create:
- Method - Create a custom variable
- Method - Create multiple custom variables
- Method - A variable for filtering
Requirements
- Comfort with R code. See How to Learn R if you're new to R or new to working with R in Displayr.
- Familiarity with Variable Set Structures and Value Attributes, see Variable Sets.
- Knowledge of how to check custom R code will be helpful: How to Troubleshoot R Code in Displayr.
Method - Create a custom variable
- In the Data Sources tree, hover over any variable and click + > Custom Code > R > and select the structure of the variable you want to create. See Variable Set Structures & Question Types for more information about variable set structures.
- Enter your R code into the Code Editor at the top. Note that because you are essentially creating a new column of data in your data set, the results calculated in R must have a value for each record in your data set (i.e., the length of the result needs to be the number of cases in your data set). In this example, we've chosen to make a Numeric variable; any results that aren't numeric or logical (TRUE/FALSE) values will be converted to missing data. TRUE/FALSE values will be converted to 1/0 automatically. The example below creates a new variable with a value of "1" for each case, and uses the length of one of the other variables, Q1, in the data set to ensure the result is the appropriate length.
- In the object inspector, click the Calculate button to run the code and create the R variable.
- On the General tab, update Name and Label as appropriate.
- OPTIONAL: From the Data tab, click the Labels button to modify the Label for each value (you can paste them in, in bulk by clicking on the Import button). This is particularly helpful if Nominal was selected as the variable's structure at Step 1.
- OPTIONAL: If the result is numeric and you wish to use this variable as a weight, in the Data tab, check Usable as a weight.
- OPTIONAL: Use this new R variable as a filter by checking Data > Properties > Usable as a filter.
Note about creating nominal variables
Nominal variables in Displayr are converted to factors in R, but unlike Displayr, R cannot assign negative or specific values to categories, so some manual work may need to be done to the Value Attributes after creating the variable. Here are some possible solutions depending on your specific needs:
| Scenario | Method |
| The underlying value of your categories doesn't matter. | At Step 1, select + > Custom Code > R > Nominal. Note you can order your categories on a table manually with this method. |
| You need underlying values starting at 1 that are consecutive for your categories. |
At Step 1, select + > Custom Code > R > Numeric. In your code, your results will be the category labels. Then, in the last line, you will convert those to a factor and specify the order of the categories to align with the value you want to assign (also the default order in tables): factor(results, levels = c("Cat 1","Cat2"))
You will Calculate the code and then change the Structure > Nominal to make it categorical. |
| You need underlying values that are not consecutive starting at 1. | At Step 1, select + > Custom Code > R > Numeric. In your code, your results will be the numeric values for the categories you want. You will Calculate the code and then change the Structure > Nominal to make it categorical. Then you will click the Labels button to modify the Label for each value (you can paste them in, in bulk). |
Method - Create multiple custom variables
In Displayr, you can use R code to create a variable set consisting of multiple numeric or text variables using only a single script of R code. This can speed up efficiencies, as opposed to writing separate codes and creating each R variable individually. This feature is accessed by hovering over any variable in the Data Sources tree and clicking + > Custom Code > R > Numeric-Multi or Text-Multi, depending on your needs. Then you will be prompted for how many R variables to make. These will be created as blank data using default code, which you can replace entirely or add to.
You will need to write R code to create the new variable(s) that produces a single matrix where each column becomes a new variable. All of the new R variables will share the same block of code.
In this example, we will be creating our new R variables from three existing variables in our example data set:
- Q1: Aided Brand Awareness
- Q2: Eaten / Bought last month
- Q3: Ever eaten
- In the Data Sources tree, select "Q1: Aided brand awareness" and click + > Custom Code > R > Numeric - Multi to create a Numeric - Multi variable set or select + > Custom Code > R > Text - Multi to create a Text - Multi variable set. The example below creates Numeric variables.
- Type the number of variables you want to create in the How Many Variables Do You Want to Create? box. In this example, we will create 4 new variables.
-
Click OK.
The results are as follows:
Each of the variables in this variable set shares the same R template code, which is used to create empty variables.The R template code looks like the one below. You can access it by clicking R Code - Data > Edit Code in the object inspector. It creates an empty data.frame (table) with as many rows as cases in your data set and as many columns as specified in Step 3. You can then add in your custom code and modify the empty table, new.data, as needed to get your desired result:
Or you can replace the entire template code completely if you are familiar with R and know that your final output is the required number of rows (cases) and columns (variables). -
The next step is to update the template code with R code of your own. In the Data Sources tree, click on Variable 1 in the New R Variable Set.
-
The default code will be in the object inspector > R Code - Data > Edit Code.
Unless you are replacing the code entirely with custom code that produces a final output which is the required number of rows (cases) and columns (variables), you should insert your code on line 17 and ensure that the final output of the code is called new.data (as shown in the example code below). In this example, the R code looks like this:
#list the variable names from each variable set that we are interested in #these are going to be the "columns" of the variable set we use #to use all variables, remove the bracket part of the code #on the data1 and data2 lines below columns = c("Burger Shack", "Burger Chef", "Nuovo Burger", "Lucky's Pizza") #use the list above to only pull of data for those variables from each variable set #you can delete the backticks and name within below and click on your #variable set to insert a reference to a different variable set #to use all variables in the set delete [,columns] on the next 2 lines data1 = `Q2 Eaten / bought last month` [,columns] data2 = `Q3 Ever Eaten` [,columns] #use the two variable sets to calculate the new calculated variables new.data = data1 + 0.5 * data2 #change the column headers in the new.data to the variable/column names from data1 colnames(new.data) = colnames(data1) #return the final table new.data -
Click Calculate.
The results are as follows. The variable names were updated with the column names we specified in the R code:
In addition, each of the new R variables shares the same code. For example:
At this point, you may want to change properties of the variable set, like Structure, etc, as needed. - Drag the New R Variable Set onto the page.
The results are as follows:
Other Considerations
In Step 5, we suggest inserting your custom code on Line 17 and using the empty new.data table as your final output. Know that you can replace the entirety of the code with your own custom code as long as the final output is a matrix or data frame with the same number of rows as your data set and the same number of columns as you specified in Step 2.
Method - A variable for filtering
Follow Method - Create a custom variable above for creating a new custom variable. You can use this new R variable as a filter by checking Data > Properties > Usable as a filter. This will filter out any result where:
- For Numeric and Nominal variables, the underlying Value in the Value Attributes is greater than 0. If results are formatted as logical data (TRUE and FALSE), those values will automatically be converted into binary values (1s and 0s) to filter in records where the result is TRUE.
- For Binary-Multi variables, the category is one where Count this Value is checked. This will default to 1 if the result from the code is binary 0s and 1s or TRUE if the results are formatted as logical data. If results are in a different format, Displayr will guess at which results to include, so you need to review the Value Attributes to confirm.
Next
R's Vectorized Math and Custom Variable Creation
How to Create a New Variable Based on Other Variables Using R
How to Band Numeric Variables Using R
How to Use R Code to Create a Filter Based on Single-Response Questions
For a more general overview of using R in Displayr, see the Displayr Help section on R.