filter dates tidyverse in r

To filter dates in the tidyverse, you can use the filter() function along with the functions from the lubridate package. The first step is to convert your date column into a date-time format using ymd() for "year-month-day" format, mdy() for "month-day-year" format, dmy() for "day-month-year" format and so on.

For example, let's say your data frame is called df and you have a column called date with dates in "month-day-year" format. You can use the following code to filter for dates between January 1st, 2018 and December 31st, 2018:

main.r
library(tidyverse)
library(lubridate)

df %>%
  mutate(date = mdy(date)) %>%
  filter(date >= ymd("2018-01-01") & date <= ymd("2018-12-31"))
141 chars
7 lines

In this code, we first used mutate() to convert the date column to a date-time object. Then we used filter() to select only the dates that fall within the specified range.

You can modify the dates and format to match your specific needs.

gistlibby LogSnag