How can I check if a given date falls between two specified dates in R?

To check if a given date falls between two specified dates in R, you can use the “between” function from the “dplyr” package. This function compares a given date to a range of dates and returns a logical value, indicating if the given date falls within the specified range. The syntax for this function is “between(date, start_date, end_date)”, where “date” is the date to be checked and “start_date” and “end_date” are the specified range. This function is useful for handling date data in R and can aid in data analysis and decision making.

Check if Date is Between Two Dates in R


You can use the following methods to check if a date is between two specific dates in R:

Method 1: Create New Column that Checks if Date is Between Two Dates

df$between <- ifelse(df$date >= start_date & df$date <= end_date, 1, 0)

Method 2: Subset Data Frame for Rows where Date is Between Two Dates

df[df$date >= start_date & df$date <= end_date , ]

Both methods assume that start_date and end_date are string variables that contain specific  dates.

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(date = as.Date('2023-01-01') + 0:9,
                 sales = c(12, 14, 7, 7, 6, 8, 10, 5, 11, 8))

#view data frame
df

         date sales
1  2023-01-01    12
2  2023-01-02    14
3  2023-01-03     7
4  2023-01-04     7
5  2023-01-05     6
6  2023-01-06     8
7  2023-01-07    10
8  2023-01-08     5
9  2023-01-09    11
10 2023-01-10     8

Example 1: Create New Column that Checks if Date is Between Two Dates

The following code shows how to create a new column named between that returns either 1 or 0 to indicate if the date in the date column is between 2023-01-04 and 2023-01-08:

#specify start and end dates
start_date <- '2023-01-04'
end_date <- '2023-01-08'

#add new column that checks if date is between start and end dates
df$between <- ifelse(df$date >= start_date & df$date <= end_date, 1, 0)

#view updated data frame
df

         date sales between
1  2023-01-01    12       0
2  2023-01-02    14       0
3  2023-01-03     7       0
4  2023-01-04     7       1
5  2023-01-05     6       1
6  2023-01-06     8       1
7  2023-01-07    10       1
8  2023-01-08     5       1
9  2023-01-09    11       0
10 2023-01-10     8       0

The new between column contains a 1 if the corresponding date in the date column is between the specified start and end dates or a 0 otherwise.

Note: We chose to return the values 1 and 0, but feel free to return whatever values you’d like using the ifelse function.

Example 2: Subset Data Frame for Rows where Date is Between Two Dates

The following code shows how to subset the data frame to only contain rows where the date in the date column is between 2023-01-04 and 2023-01-08:

#specify start and end dates
start_date <- '2023-01-04'
end_date <- '2023-01-08'

#subset data frame where rows are between start and end dates
df[df$date >= start_date & df$date <= end_date , ]

        date sales between
4 2023-01-04     7       1
5 2023-01-05     6       1
6 2023-01-06     8       1
7 2023-01-07    10       1
8 2023-01-08     5       1

Notice that the resulting data frame only contains rows where the date in the date column is between the specified start and end dates.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Plot a Time Series in R
How to Extract Year from Date in R
How to Aggregate Daily Data to Monthly and Yearly in R

x