Quantcast
Channel: Create a Vector of All Days Between Two Dates - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by LMc for Create a Vector of All Days Between Two Dates

$
0
0

There are a few ways you can do this using the clock package.

There is a sequencing function like many other answers, however with this function you have a finer control over the sequencing:

library(clock)# every day between two datesdate_seq(from = Sys.Date(), to = Sys.Date() + 5, by = duration_days(1))# [1] "2024-03-07" "2024-03-08" "2024-03-09" "2024-03-10" "2024-03-11" "2024-03-12"# every other day between two datesdate_seq(from = Sys.Date(), to = Sys.Date() + 5, by = duration_days(2))# [1] "2024-03-07" "2024-03-09" "2024-03-11"# every 3rd month until total size is 3# can do arithmetic in duration function and # provide explicit resolution to invalid datesdate_seq(from = date_build(2024, 3, 31), by = duration_months(sqrt(9)),          total_size = 3, invalid = "previous-day")# [1] "2024-03-31" "2024-06-30" "2024-09-30"

There is also a set of spanning functions that will span from the min and max of a vector:

x <- date_build(2024, c(3, 3, 3), c(7, 8, 11))# [1] "2024-03-07" "2024-03-08" "2024-03-11"# spans from min 2024-03-07 to max 2024-03-11date_spanning_seq(x)[1] "2024-03-07" "2024-03-08" "2024-03-09" "2024-03-10" "2024-03-11"

date_spanning_seq uses day precision, so there is not as much control. calendar_spanning_seq is available where you can provide more control over how to span.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images