There are many ways in which you can create a filter in Displayr, see this article for a guide. For filters which only compare variables against a given value (i.e. Q1 == "Male"), the filtering tool interface is the easiest method. However, in more complex scenarios creating a filter may require basic coding. You will need to use code when the filter logic :
- Is different based on a condition (i.e. if Q1 is blank use Q2)
- Requires calculations (i.e. if Q1 is higher than the average)
- Compares variables against one another (i.e. where Q1 is larger than Q2)
This article describes how to use simple R code to create a filter using single-response variable sets.
Requirements
- These examples all use at least one single-response variable in the logic. However, you can write more complex examples to use multi-variable variable sets to filter as well.
- Understanding of common R conditional operators (below), see How to Work with Conditional R Formulas for more detail:
Method
- Hover over a variable in the Data Sources tree and click + > Custom Code > R > Numeric. A new numeric variable called newvariable will appear in the Data Sources tree, and the Code Editor for the R variable will appear.
- Enter the code for the filter in the Code Editor, see below for examples. When you are done typing the variable will automatically recalculate. If there are any warnings or errors and you need to troubleshoot the code, you should copy it into a Calculation > Custom Code output and check Show raw R output to see where the code is breaking, see How to Troubleshoot R Code in Displayr.
- The R code should return a TRUE, FALSE, or NA (missing) value for each case in the data. These will be converted to 1, 0, and NA (missing values) because the variable is Numeric. All cases with a TRUE (1) will be filtered in on outputs where the filter is applied.
- In the object inspector, 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 under Data > Properties.
Examples
Below are examples of using R code to create filters. Keep in mind that when creating logic for categorical data (i.e. Nominal or Ordinal variables) the value Label and not the actual value is what R will use to evaluate the code. Also note that if there is missing data in the variable instead of using == or != use the %in% operator and !.
Logic is different based on a condition
EXAMPLE: If Q1 is blank use Q2 to see what cases are greater than 3. We use the ifelse() function instead of just if and else because we need the logic to run on each case in the data set, more detail is here.
#the function works like ifelse(condition, code to run if true, code to run if false) ifelse(is.na(Q1), Q1 > 3, Q2 > 3)
EXAMPLE: You can use if and else to make a decision tree of what logic to use for a filter. This is common for filters where the variables and values used may be determined by a Control (Combo Box or List Box). For example, Combo.box.1 determines if filtering is by Wave or Since 2024. Combo.box.2 is the selection of waves to filter in. If Since 2024 is selected, the years 2024 and above will be shown.
#if filtering by wave compare the wave to periods selected
if(Combo.box.1 == "Wave"){
waves %in% Combo.box.2
#if filtering since 2024 return cases from 2024 and after
} else if(Combo.box.1 == "Since 2024") {
#this assumes years are Numeric
years >= 2024
}
Logic requires calculations
EXAMPLE: Each Q1 response is NOT greater than the average across respondents of Q2, see How to Perform Mathematical Calculations Using R for more mathematical functions:
!(Q1 > Average(Q2))
EXAMPLE: Ensure a list of variables adds up to 100 (useful for validating "share of" variables). Note this can also be done without writing code by selecting the variables, clicking + > Calculate Across Variables > Sum, then using that new variable inside the filtering tool to flag it when equal to 100. You can also do this in R as well. Because R can do vectorized math (see here), you can also add the variables directly together and compare, Perform Mathematical Calculations Using R for more mathematical functions:
(Q1_1 + Q1_2 + Q1_3) == 100
You can also use a special function, SumEachRow() , because each "row" is a respondent. So this will sum the variables for each respondent and check against 100.
#cbind joins the variable columns together to calculate across each row
SumEachRow(cbind(Q1_1, Q1_2, Q1_3)) == 100
If the variables are a part of the same variable set, you can reference the variable set label in back ticks `` to pull in a table of all variables at once.
#put variable set in backticks
SumEachRow(`Shares of wallet`) == 100
Logic compares variables against each other for each respondent
EXAMPLE: The Q1 response is less than the Q2 response of the respondent. For example to filter in respondents who said their monthly pay (Q1) was less than their annual salary (Q2) for validation purposes:
Q1 < Q2
EXAMPLE: Only filter in respondents who answered with the same brand for two questions that were asked.
Q1 == Q2
Next
How to Work with Conditional R Formulas
How to Delete Cases From a Data Set
How to Create a New Variable Based on Other Variables Using R
How to Create Filters Using Variables in Your Data