This article describes several ways to create filters in Q and how to use them in tables.
Requirements
- A variable to be used as a filter.
Methods
Tagging existing variables
Any variable in the Variables and Questions tab can be tagged as a filter variable by pressing the button. Any non-missing, positive values (i.e., greater than 0) are included in the filter.
- Select the Variables and Questions tab
- Click the button the enable the filter:
Quick method
The most straightforward approach to creating a filter is to select the cells on a table that correspond to the filter you wish to create, and then right-click and select Create filter.
- Select the table you want to create the filter from.
- Select the cells that correspond to the filter you want to create. In this example, respondents are selected if they prefer Coke products and are under 30 years old:
- Click the button in the toolbar
- Change the name of the filter (if desired)
- Uncheck the Apply filter to the current table box if you do not want to apply the filter to the current table.
- Click OK.
Note that when you create a filter using this method, it is automatically created as a Binary - Complicated Variable, and is found at the top of the Variables and Questions tab, where it can be edited (by right-clicking on it).
Binary - complicated variables
A filter can be created using any type of data using Create > Variables and Questions > Variable(s) > Binary - Complicated Variable. See Binary Variable or the relevant Online Training modules listed at the top-right of this page for more information.
- From the toolbar, select Create > Variables and Questions > Variable(s) > Binary - Complicated Variable
- Specify a logical expression. In this example, persons aged 30 and over were selected.
- Specify a name for the filter variable.
- Click OK.
To learn more about this method, please see: Binary Variable
Quickly creating filters using an existing question
For example, if you want to partition your dataset into test & train samples, you would do the following:
- From the toolbar, select Automate > Browse Online Libary > Filter > Filter for Train-Test Split
- Specify the size of your training sample, in this case 70%
- Click OK.
Logic variables
These variables use a short and easy-to-use syntax for creating variables. For example, each of the following lines describes a filter, and in many cases the same filter (i.e., illustrating that there are lots of different ways of creating a filter using logic).
To create a logic variable:
- From the toolbar, select Create > Variables and Questions > Variable(s) > Logic Formula
- For example:
useCoke useCoke(1) NOT useCoke(0) age(#1, #2, #3, #9) age([18 to 24], [25 to 29], [30 to 34], [65 or more]) age(1-3, 9) age([18 to 24]-[30-34],[65 or more]) age(#1-#3, #9) age(1-3) OR age(9) age <= 3 OR age = 9 NOT age([35-44]-[60 to 64]) age >= [55-64] AND {occupation = [Retired] OR gender = [Female]}
JavaScript
Any JavaScript Variable can be used as a filter.
Rules and QScripts
The Online Library (Automate > Browse Online Library > Filters) contains various QScripts and Rules for creating filters.
Applying filters
Once a filter has been created, it can be applied to any table using the Filter drop-down on the bottom-left of the screen. Each time you apply a filter to a table using a Filter Drop-down Menu at the bottom of the Tables/Outputs Tab, another filter drop-down will appear to its right so that you can easily apply further filters to the same table. For example:
There are two ways of seeing that a filter has been applied. First, the drop-down displays the name of the filter with a green background. Second, the description of the sample size at the base of the screen also shows whether or not a filter has been applied.
To remove a filter, go to the Filter drop-down and select Total sample or press .
Managing Filters
In larger projects, it can sometimes be tricky to change a filter across a large number of tables, particularly when the filter is only applied to some of the tables, or the filter is applied in a different position on different tables. The QScript Filtering - Change Filters on Selected Tables can help save a lot of time when changing filters in a large project.
Next
How to Create a Complicated Filter in Q