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.
- 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.
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 Inputs > 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:
QFilter in the rows parameter of your R code:
As this is a single variable, we simply need to place
QFilter within square brackets. If your output includes columns then it would be
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: dropdown,
QFilterwill be a series of
FALSEvalues 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,
QFilterwill return with a final series of
FALSEvalues evaluated by combining the filters using the
ANDoperator, 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
FALSEvalues automatically updates when the filters change in the dropdown.
- If no filters are applied, it is a single value of
- If you apply filters to your Calculation via Inputs > FILTERS & WEIGHT > Filter(s) but do not include
QFilterin 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.