This article describes how to use R code with a Date/Time variable to create dynamic custom variables that can be used as filters.
Requirements
A Date/Time variable. In this example, we use a date variable called StartDate. Data in R is in the format YYYY/MM/DD as seen below. For more info on how to get your string into a Date/Time variable, see How to Easily Convert Strings to Times and Dates in R with flipTime.
Method
In general, the steps for creating binary R variables are as follows:
- Hover over any variable in the Data Sources tree and click Plus (+) > Custom Code > R > Numeric. (Note this type is used if you only plan on using the variable as a filter. Otherwise, you may want to use R > Binary-Multi (to show selected respondents as a column in a table) or Nominal (to show both selected and unselected in a table, see method 7. Date categories below.).
- OPTIONAL: Tick General > Usable as a filter.
- Paste your code into the R Code editor.
Below are some examples of the types of date-based conditions that you can easily create.
1. Before/After Date
StartDate is before October 2020:
StartDate < "2020/10/01"
Here, the date format must match YYYY/MM/DD.
You can also create a categorical variable to label the pre and post period with similar logic, if needed. See
2. Date Range
StartDate is between October and December 2020:
StartDate >= "2020/10/01" & StartDate <= "2020/12/31"
Again, the date format must match YYYY/MM/DD.
3. Last/Next K period
StartDate is within the last 3 months:
library(lubridate)
StartDate >= Sys.Date() - months(3)
- Here, the months argument is from the lubridate package, so we need to reference this library.
- This can be changed to days, weeks, or years, and you can change the – to a + for future periods.
- Sys.Date() is simply the system date or the current date on the server.
4. Previous/Current/Next period
StartDate falls within the current quarter:
library(flipTime)
Period(StartDate, "quarter") == Period(Sys.Date(), "quarter")
- Here, the Period argument is from our flipTime package, so we need to reference this library.
- The quarter, as defined by StartDate is then compared to the current quarter based on today's date.
- The quarter argument can be changed to day, week, month, or year.
- You can also add the period arguments from the previous example to adjust when the period falls. For example, comparing StartDate to next quarter is as simple as adding 3 months to today's date:
library(flipTime)
library(lubridate)
Period(StartDate, "quarter") == Period(Sys.Date() + months(3), "quarter")
5. Date Filtering
The 2. Date Range code above can be further expanded on to use Date Controls (see How to Insert a Date Control). Where your Start Date control is named Date and your End Date control is named Date.2, you can use the following code in a new R filter variable (similar to How to Connect Filters to Controls Using R Code) to select responses that have a StartDate during that range:
DateFilter = StartDate >= Date & StartDate <= Date.2
Below is an example of the raw StartDate and DateFilter data to show how the dates will be filtered (the 1s in the DateFilter column) based on the control boxes above:
You would then use the DateFilter as a filter on your tables and outputs that you'd like to filter.
7. Date categories
If you'd like to use the date filter as a filter as well as see the groups for "Selected" and "Not selected" in a table, you can create a Nominal variable and add labels to the date categories as you wish. For example to create a variable that shows a Pre and Post period based on a date and filters in the post period:
- Hover over any variable in the Data Sources tree and click Plus (+) > Custom Code > R > Nominal.
- OPTIONAL: In the object inspector, check General > Usable as a filter.
-
Paste your code into the R Code editor. For Nominal variables, you will need to either edit the category Label or Value in the object inspector > Values after the code is calculated -- see sections below.
To specify the category Label outside the R code in the Value Attributes:
The R Code should return a TRUE/FALSE value or series of numbers corresponding to the category number (numbers above 0 will be filtered in). For example
##Create a date filter for post Feb 1 2019 showing both categories
StartDate < "2019/02/01"Then you will add the labels to the 0 (filtered out) and 1 (filtered in) values using the object inspector > Labels button.
To specify the category Value outside the R code in the Value Attributes:
If it's clearer for you to use category labels in the R code, you can use those in your R code logic and then convert them to categories using the factor() function. If you'd like to specify the order of the categories, you can using the levels argument as in the example below:
##Create a date filter for post Feb 1 2019 showing both categories
#replace StartDate with the name of your date variable
datecats=ifelse(StartDate < "2019/02/01","Pre Feb 2019","Post Feb 2019")
#convert strings to categories and give them certain values using Value button
factor(datecats,levels=c("Pre Feb 2019","Post Feb 2019"))Then you can specify the individual values for the categories using the object inspector > Values button. Give any category you want filtered out, a 0 value and other categories values of 1 or more, and click OK.
Next
How to Work with Aggregated Dates Using R
How to Easily Convert Strings to Times and Dates in R with flipTime
How to Use R Code to Show the Date and Time
UPCOMING WEBINAR: 10 Market Research Predictions Over the Next 4 Years