What Is the Best Way to Filter by Date in R?

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post What Is the Best Way to Filter by Date in R? appeared first on Data Science Tutorials

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.

The post What Is the Best Way to Filter by Date in R? appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)