There are many ways in which you can create a filter in Displayr. In some more complex scenarios creating a filter may require basic coding. You must do this if you want your filter to incorporate some sort of calculation across respondents or comparison of responses within each respondent. This article describes how to use simple R code to create a filter based on a single-response question.
Requirements
Please note this requires the Data Stories module or a Displayr license.
- A data set containing at least one single-response variable.
- Understanding of common R conditional operators (below), see How to Work with Conditional R Formulas for more detail:
Method - Create an R filter variable
- Hover over a variable in the Data Sets tree and click on the + that appears then Custom Code > R - Numeric. A new numeric variable called newvariable will appear in the Data Sources tree.
- Change the Label and Name of the variable in the object inspector via General > GENERAL to the desired text (e.g., Filter_variable).
- Check Usable as a filter.
Method - Create the R filter logic
- Under Data > R CODE of your filter variable, enter the R code matching your binary filter condition. If the condition is met, the variable will return a value of 1 (which will be used to filter your data). If the condition is not met, the variable will return a 0 value.
- Note, instead of typing the variable name, you can select the variables you wish to include in your R code and drag them from the Data Sources tree into Data > R CODE for your filter variable.
- With R you can reference either the variable name or the variable set name. In cases when a variable name contains spaces, the label needs to be wrapped using ` `. For example, if we have a variable called "Q1" with the label of "Q1. How old are you?", in R we can reference this as either Q1 or `Q1. How old are you?`.
Examples
Below are examples of using R code to create filters:
d1 response equals the 25 to 29 category - note if there is missing data in the variable use the %in% operator below:
d1 == "25 to 29"
d1 response equals the 25 to 29 or 45 to 49 category (or more):
d1 %in% c("25 to 29", "45 to 49")
Q1 response equals 1:
Q==1
Q1<5 | Q2==2
Q1 is greater than or equal to 25 AND less than or equal to 100:
Q1>=25 & Q1<=100
And here are the reverse of the above filter conditions using the ! operator.
Q1 response is not 1:
Q1!=1
Q1 is NOT less than 5 AND Q2 is NOT 2:
!(Q1<5 | Q2==2)
Q1 is NOT greater than or equal to 25 AND NOT less than or equal to 100:
!(Q1>=25 & Q1<=100)
Each Q1 response is NOT greater than the total sum of Q2, see How to Perform Mathematical Calculations Using R for more mathematical functions:
!(Q1>Sum(Q2))
The Q1 response is less than the Q2 response of the respondent:
Q1 < Q2
See Also
How to Work with Conditional R Formulas
How to Filter Cases in the Data Editor Using R
How to Create a New Variable Based on Other Variables using R
How to Create a Binary Filter from Selected Data
How to Tag a Variable as a Filter