There are many ways in which you can create a filter in Q. In some more complex scenarios creating a filter may require basic coding. This article describes how to use simple R code to create a filter based on a Pick One question.

## Requirements

- A data set containing at least one single-response variable.
- Understanding of common R conditional operators, for detail see How to Work with Conditional R Formulas:

## Method

- On the Variables and Questions tab, right click on a variable and select
**Insert Variable(s) > R Variable**. - Under
**Properties > R CODE**, enter the R code matching your criteria (see examples below). - Click the
**play button**to run the code. - Preview your results on the left pane shaded in the blue columns with input data used shaded in grey. If the condition is met, the variable will return a check for true (which will be used to filter your data). If the condition is not met, the variable will return an X for false. For example:
- Set the
**Variable Base Name**(if desired) and**Question Name**at the bottom of the window and click**Add R Variable***.* - Click on the
**F**in the**Tags**column to make this variable available as a filter.

## Examples of Filter Logic

Below are examples of using R code to create filters:

d1 response equals the* 25 to 29* category - note if there is missing data in the variable use the %in% operator below:

d1 == "25 to 29"

d1 response equals the *25 to 29* or *45 to 49* category (or more):

`d1 %in% c("25 to 29", "45 to 49")`

Q1<5 | Q2==2

Q1 is greater than or equal to 25 AND less than or equal to 100:

Q1>=25 & Q1<=100

And here are the reverse of the above filter conditions using the ! operator.

Q1 response is not 1:

Q1!=1

Q1 is NOT less than 5 AND Q2 is NOT 2:

!(Q1<5 | Q2==2)

Q1 is NOT greater than or equal to 25 AND NOT less than or equal to 100:

!(Q1>=25 & Q1<=100)

## Next

How to Work with Conditional R Formulas

How to Create a New Variable Based on Other Variables using R

How to Filter Raw Data Using R

## Comments

0 comments

Article is closed for comments.