This feature allows you to use R code to create a variable set consisting of multiple numeric or text variables. This enables you to make multiple R variables at once by writing a single piece of R code, as opposed to writing separate code and creating each variable individually.
- Familiarity with programming using R. How to Learn R is a good starting point if you're new to R or new to using it in Displayr.
- Knowledge of how to check custom R code will be helpful: How to Troubleshoot R Code in Displayr
- 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 data set:
- Q1: Aided Brand Awareness
- Q2: Eaten / Bought last month
- Q3: Ever eaten
- Click the plus sign below the variable set Q1: Aided brand awareness
- From the Insert Variable(s) menu, select Custom Code > Multiple R Variables > Numeric to create a Numeric - Multi variable set or select Custom Code > Multiple R Variables > Text 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, that is used to create empty variables.
The R template code looks like the below. 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 Set tree, click on Variable 1 in the New R Variable Set.
- The current code will be in the object inspector > Properties > R CODE box.
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
- 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:
In step 6 we suggest inserting your custom code on line 17 and using the empty new.data table as your final output. Do 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 3.
How to Troubleshoot R Code in Displayr
Article is closed for comments.