If your Calculation uses variables or a raw data table as an input (as opposed to a table/visualization/other output), you can use the standard Filter(s) dropdowns to quickly filter your R Output. This article describes how the QFilter
object can be used within your R code to apply filters to respondent-level data.
Requirements
Please note these steps require a Displayr license.
- A Calculation that uses variables from the data set or an unfiltered raw data table.
- One or more filter variables from the same data set.
Method
1. In the toolbar, go to Calculation > Custom Code. In this example, we will just enter Age
so that it returns the age group of each individual record in our data set. There are 800 records in total.
2. Under Data > Filters & Weight > Filter(s), apply the desired filter variable(s). If you apply multiple filters, a single variable is effectively created using an AND operation. Here, we will apply a Female filter:
3. Enter QFilter
in the rows parameter of your R code:
Age[QFilter]
As this is a single variable, we simply need to place QFilter
within square brackets. If your output includes columns then it would be [QFilter,]
instead.
Our Calculation now returns 405 records, the same number as females in our data set.
Note the following:
- If a filter(s) is selected in the Filter(s) dropdown,
QFilter
will be a series ofTRUE
andFALSE
values as many cases as is in your data set. You can then use this to filter the raw data/variables being used in your calculation using this variable. - If multiple filters are selected,
QFilter
will return with a final series ofTRUE
andFALSE
values evaluated by combining the filters using theAND
operator, i.e. if both male and under 18 filters are applied, the final values will reflect cases that are male AND under 18. - The series of
TRUE
andFALSE
values automatically update when the filters change in the dropdown. - If no filters are applied, it is a single value of
TRUE
. - If you apply filters to your Calculation via Data > Filters & Weight > Filter(s) but do not include
QFilter
in your code, you will receive the following warning: - The Variable Label is available as an attribute (i.e.,
attr(QFilter, "label")
). Where there are multiple filters, they are concatenated as a list (e.g., "Male, 18 to 24 and Tall"). - The Variable Name is available as an attribute (i.e.,
attr(QFilter, "name")
). Where there are multiple names, they are concatenated as with labels.
Next
How to Add a Custom Calculation
How to Work with R in Displayr