Filtering is the process of selecting part of your sample, and then creating a table, calculation, or other analysis using only that sample. It is sometimes called ‘subsetting’ or ‘drilling-down’.

You can apply a filter in Displayr to any analysis, restricting it to a subset of interest. You can specify a filter when the document is in Edit mode as well as making it available for end-users of the document. These end-users can choose which filter groups to consider while in View mode. In this article we review the range of options available for creating filters in Displayr, and how to apply filters to a table or other analysis.

Applying filters in Edit mode

In Edit mode, both individual outputs and entire pages can be filtered using the Inputs > FILTERS & WEIGHTS > Filter(s) menu in the object inspector on the right side of the screen. When you select a filter variable, it appears in green. You can select multiple filters in each menu.

Filtering data

Applying filters in View mode

Anyone viewing a document can filter each page of the published document. To do so, use the Filters menu on the top right of the screen.

filtering data

Making variables available to filter

Let's say there are binary variables in your data set that you want to use for filtering. You can make these available as filters by:

  1. Select the relevant variable set(s) under Data Sets in the bottom left.
  2. Tick the Usable as a filter box on the right of the screen.

Displayr will include any cases with a value greater than 0 in the filter. Thus, it is usually appropriate to use variables which are binary.

filtering data

Combining filters

You can apply more than one filter at a time in both Edit and View mode. The result of applying two filters depends on the setup of your data. Also, you can set your data up in such a way as to allow both of the following:

  • When Filter 1 and Filter 2 are selected, show me data for cases that are only included in both
  • When Filter 1 and Filter 2 are selected, show me data for cases that are in either of the two filters.
Behavior Variable Setup Appearance in Filter menu Appearance in Data Sets
Include cases that are in both filters Filters are in different variable sets
Include cases that are in either filter Filters are in the same variable set

You can group filter variables by:

  1. Highlighting the variables under Data Sets.
  2. Selecting Data Manipulation > Variables > Combine.
  3. Making the variable set available for filtering as above.

On the other hand, you can separate grouped filter variables by:

  1. Selecting the variable set under Data Sets.
  2. Selecting Data Manipulation > Variables > Split.
  3. Making the variable set available for filtering as above.

Creating simple filters

Simple filters can be created using Insert > Variables > Filter. This dialogue allows you to:

  • Select categories from a variable or variable set.
  • Pick combinations of categories from a pair of variables or variable sets.
  • Select data which falls in a numeric range.

How to filter data

For example, the image above shows filter creation based on two categorical variables, Age and Gender. The selections indicate that the new filter variable will select all the cases in the data set corresponding to people who are Male and aged 18 to 29.

Formula variables

Even more flexible options for constructing filters are available if you write a formula in R to describe the respondents that are of interest. To add a variable to your data set:

  1. Select Insert > Variables > R > Numeric Variable
  2. Enter your expression in the R CODE section of the object inspector on the right
  3. Click Calculate.

Here, you should design a formula which returns a vector of 0’s and 1’s which has one value for each case in the data set. A 0 indicates the case should not be included in the filtered sample while a 1 indicates that the case should be included. Once you have created a variable, you must tag it as a filter, like above. For further examples of R-based filters, see How to Use Basic R Code for Creating Filters.

Creating filters automatically

To generate filters for each category in a variable set, select the variable set under Data Sets, and use Insert > Variables > Filter > Filters from Selected Data. If any categories in the variable set have been merged (via a table), Displayr will have the new filters reflect the merged categories.

Applying filters to raw data

You can remove cases from your raw data using filters. This is particularly useful when there are cases in the data set which should never be include in any of your analyses (for example, to remove data entry errors).

To delete cases in this way:

  1. Select the name of your data set under Data Sets.
  2. Select a unique ID variable under Properties > Inputs > Unique identifier on the right of the screen. Displayr needs a variable with unique values to keep track of the cases in your data set.
  3. Select Properties > INPUTS > Delete observations....
  4. Choose the filter variable which identifies the cases you want to remove and select Delete.

You can return deleted cases to the sample by using the same option and selecting Undelete All.

Find out how to do more in Displayr by checking out the "Using Displayr" section of our blog.