I have date column which runs from 2020-01-01 - 2022-02-01, within a data frame. Is there a way to select all rows where the date is the 5th day of every month between 2020-01-01 - 2022-01-01, i.e. 2020-01-05, 2020-02-05, 2020-03-05...2022-01-05?
example dataframe, of everyday of the last 1000 days.
filtering it for the 5th day of month , betwwen may and september 2020
library(tidyverse)
library(lubridate)
(start <- tibble(d=seq.Date(from=Sys.Date()-1000,
to = Sys.Date(),
by = 1)))
(end <- filter(start,
day(d)==5,
between(d,
left=date("2020-05-05"),
right=date("2020-09-05"))))
Here is one way to filter using substr()
.
library(tidyverse)
df = data.frame(mydate = seq.Date(from = as.Date('2020-01-01'),
to = as.Date('2022-02-01'),
by = 'day'))
# filter based on substring
df %>% filter(substr(mydate, 9, 10) == '05')
#> mydate
#> 1 2020-01-05
#> 2 2020-02-05
#> 3 2020-03-05
#> 4 2020-04-05
#> 5 2020-05-05
#> 6 2020-06-05
#> 7 2020-07-05
#> 8 2020-08-05
#> 9 2020-09-05
#> 10 2020-10-05
#> 11 2020-11-05
#> 12 2020-12-05
#> 13 2021-01-05
#> 14 2021-02-05
#> 15 2021-03-05
#> 16 2021-04-05
#> 17 2021-05-05
#> 18 2021-06-05
#> 19 2021-07-05
#> 20 2021-08-05
#> 21 2021-09-05
#> 22 2021-10-05
#> 23 2021-11-05
#> 24 2021-12-05
#> 25 2022-01-05
Created on 2022-11-29 with reprex v2.0.2.9000
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.