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 numeric R variable. If the new variable will ultimately be a categorical, many times it is easier to create an R Text variable instead, see How to Create a Custom R Text Variable for how to achieve this. You can also create multiple R variables if needed, see How to Create Multiple R Variables. This article covers using a custom numeric R variable to create:
- Method - A numeric variable
- Method - A nominal (categorical) variable
- 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.
Method - A numeric variable
- There are two ways you can insert the new variable:
- Via the Data Sources tree: Scroll to where you want to insert the new variable in your data set. and hover. On hover, click Plus (+) > Insert Variable(s) > Custom Code > R - Numeric.
- Via the Anything menu: Select the Data Source in the Data Sources pane where you'd like to add your new variable. From the toolbar menu, select the Anything icon and select Data > Variables > New > Custom Code > R - Numeric. This will create a new variable at the top of your data set.
- 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). Since we've chosen to make an R-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 one 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 under General, update Name and Label as appropriate.
- 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.
Method - A nominal (categorical) variable
There are a few approaches to creating a new Nominal variable depending on what the final Value Attributes need to be. 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's some possible solutions depending on your specific needs:
Scenario | Method |
The underlying value of your categories doesn't matter. | Use the method in How to Create a Custom R Text Variable to make a nominal variable. 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. |
Follow the Method - A numeric variable above. 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. | Follow the Method - A numeric variable above. 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 Values button to modify the Label for each value (you can paste them in, in bulk). |
Method - A variable for filtering
Follow the Method - A numeric variable, and if wanted, Method - A nominal (categorical) variable. You can use this new R variable as a filter by checking Data > Properties > Usable as a filter. This will filter in 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.