To create your own custom R variables, follow these steps:
- On the Variables and Questions tab, right click on a variable and select Insert Variable(s) > R Variable.
- Under Properties > R CODE, enter your custom R code.
- Click the play button to run the code.
- Preview your results on the left pane shaded in the blue columns with input data used shaded in grey. Note if you are creating a table/matrix of data with multiple columns, each column will become an R variable in the Question automatically. A one-variable example is below:
- Set the Variable Base Name (if desired). This is useful if you want to create more than one R variable at a time. You can do this by making the final result of your R code be multiple columns of data (instead of one), which in turn get expanded to multiple variables in the data set automatically.
- Add a Question Name at the bottom of the window and click Add R Variable.
- Q will select either Text or Number as the Question Type for the variable. You should review the Question Type and change it, if necessary.
-
OPTIONAL: If the result is binary (1 or 0) or logical (TRUE or FALSE) and you wish to use this variable as a filter, click the F in the Tags column.
-
OPTIONAL: If the result is numeric and you wish to use this variable as a weight, click the W in the Tags column.
If you are simply referencing a categorical variable in your code, the labels will carry over when the Question Type is set to Pick One. However, you can also select Values and update the labels there:
Next
How to Work with Conditional R Formulas
How to Use R Code to Create a Filter Based on Single-Response Questions
How to Create a New Variable Based on Other Variables using R
How to Filter Raw Data Using R
How to Recode Data Based on a Lookup Using R