What Is the Best Way to Filter by Date in R?, Using the dplyr package in R, you can filter a data frame by dates using the following methods.
Subsetting with multiple conditions in R – Data Science Tutorials
Method 1: After Date Filter Rows
df %>% filter(date_column > '2022-01-01')
Method 2: Filter Rows Before Date
df %>% filter(date_column < '2022-01-01')
Method 3: Filter Rows Between Two Dates
df %>% filter(between(date_column, as.Date('2022-01-20'), as.Date('2022-02-20')))
With the following data frame in R, the following examples explain how to utilize each method in practice.
How to Count Distinct Values in R – Data Science Tutorials
Let’s create a data frame
df <- data.frame(day=seq(as.Date('2022-01-01'), by = 'week', length.out=10), sales=c(40, 35, 39, 44, 48, 51, 23, 29, 60, 65))
Now we can view the data frame
df
day sales 1 2022-01-01 240 2 2022-01-08 335 3 2022-01-15 359 4 2022-01-22 544 5 2022-01-29 548 6 2022-02-05 251 7 2022-02-12 223 8 2022-02-19 529 9 2022-02-26 660 10 2022-03-05 165
Example 1: Filter Rows After Date
To filter for rows in the data frame with a date after 1/25/2022, use the following code.
library(dplyr)
filter for rows with dates after 1/25/2022
df %>% filter(day > '2022-01-25')
day sales 1 2022-01-29 548 2 2022-02-05 251 3 2022-02-12 223 4 2022-02-19 529 5 2022-02-26 660 6 2022-03-05 165
Each row in the generated data frame has a date that is later than 1/25/2022.
Best Books on Data Science with Python – Data Science Tutorials
Example 2: Filter Rows Before Date
To filter for rows in the data frame with a date before 1/25/2022, we can use the following code.
library(dplyr)
Let’s filter for rows with dates before 1/25/2022
df %>% filter(day < '2022-01-25')
day sales 1 2022-01-01 240 2 2022-01-08 335 3 2022-01-15 359 4 2022-01-22 544
Each entry in the generated data frame has a date that is prior to 1/25/2022.
Example 3: Filter Rows Between Two Dates
To filter for rows in the data frame with a date between 1/20/2022 and 2/20/2022, use the following code.
library(dplyr)
filter for rows with dates between 1/20/2022 and 2/20/2022
Best Data Science YouTube Tutorials Free to Learn – Data Science Tutorials
df %>% filter(between(daty, as.Date('2022-01-20'), as.Date('2022-02-20')))
day sales 1 2022-01-22 544 2 2022-01-29 548 3 2022-02-05 251 4 2022-02-12 223 5 2022-02-19 529
The dates in the rows of the generated data frame range from 1/20/2022 to 2/20/2022.
If none of the ways above work, you may need to use them as.Date() function to convert the dates you’re working with to a recognized date format.