sequence dates lubridate in r

To sequence dates using lubridate in R, you can use the seq() function along with the ymd() function from the lubridate package.

Here is an example:

library(lubridate)

start_date <- ymd("20210101")
end_date <- ymd("20210201")

seq(start_date, end_date, by = "day")
117 chars
7 lines

In this example, we are creating a sequence of dates from January 1st, 2021 to February 1st, 2021 with a step of one day.

The output will be a sequence of dates in the format yyyy-mm-dd, like this:

main.r
 [1] "2021-01-01" "2021-01-02" "2021-01-03" "2021-01-04" "2021-01-05" "2021-01-06" "2021-01-07" "2021-01-08" "2021-01-09" "2021-01-10"
[11] "2021-01-11" "2021-01-12" "2021-01-13" "2021-01-14" "2021-01-15" "2021-01-16" "2021-01-17" "2021-01-18" "2021-01-19" "2021-01-20"
[21] "2021-01-21" "2021-01-22" "2021-01-23" "2021-01-24" "2021-01-25" "2021-01-26" "2021-01-27" "2021-01-28" "2021-01-29" "2021-01-30"
[31] "2021-01-31" "2021-02-01"
436 chars
5 lines

related categories

gistlibby LogSnag