Although Displayr automates the creation of the most commonly-required variables, sometimes there's a need to create a new variable using custom code. If the new variable will ultimately be a categorical or text variable, you can create an R Text variable to achieve this. You can also create multiple R variables for a variable set if needed, see How to Create Multiple R Variables.
Requirements
- Comfort with R code. See How to Learn R if you're new to R or new to working with R in Displayr.
Method
- Either hover over any variable in your Data Sources tree > Plus (+) > Insert Variable(s) > Custom Code > R - Text.
- 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-Text variable, any numeric values returned will be converted to characters.
- In the object inspector, click the Calculate button to run the code and create the R variable.
- In the General tab, update Name and Label as appropriate.
- OPTIONAL: If you wish to analyze this data as categories instead of raw text responses, change the Data > Properties > Structure to Nominal. This will then use the text results as category labels. Note that you can still order your categories on a drag and drop table directly, if needed.
See Also
How to Work with R in Displayr
How to Create a Custom Numeric R Variable