How to Generate a Sequence of Dates with lubridate in R


You can use the following basic syntax to generate a sequence of dates using the lubridate package in R:

seq(ymd('2022-01-01'), ymd('2022-10-31'), by='1 week')

This particular example will generate a sequence of dates from 1/1/2022 to 10/31/2022 at intervals of 1 week.

To use a different interval, simply replace week with another unit of time such as day, month, quarter, year, etc.

The following examples show how to use this syntax to generate a sequence of dates in practice.

Example 1: Generate Sequence of Dates by Days

The following code shows how to generate a sequence of dates from 1/1/2022 to 2/15/2022 by day:

library(lubridate)

#generate sequence of dates from 1/1/2022 to 2/15/2022 by day
seq(ymd('2022-01-01'), ymd('2022-02-15'), by='1 day')

 [1] "2022-01-01" "2022-01-02" "2022-01-03" "2022-01-04" "2022-01-05"
 [6] "2022-01-06" "2022-01-07" "2022-01-08" "2022-01-09" "2022-01-10"
[11] "2022-01-11" "2022-01-12" "2022-01-13" "2022-01-14" "2022-01-15"
[16] "2022-01-16" "2022-01-17" "2022-01-18" "2022-01-19" "2022-01-20"
[21] "2022-01-21" "2022-01-22" "2022-01-23" "2022-01-24" "2022-01-25"
[26] "2022-01-26" "2022-01-27" "2022-01-28" "2022-01-29" "2022-01-30"
[31] "2022-01-31" "2022-02-01" "2022-02-02" "2022-02-03" "2022-02-04"
[36] "2022-02-05" "2022-02-06" "2022-02-07" "2022-02-08" "2022-02-09"
[41] "2022-02-10" "2022-02-11" "2022-02-12" "2022-02-13" "2022-02-14"
[46] "2022-02-15"

The result is a sequence of 46 dates ranging from 1/1/2022 to 2/25/2022.

Note: Instead of typing 1 day, you could also just type day.

Example 2: Generate Sequence of Dates by Weeks

The following code shows how to generate a sequence of dates from 1/1/2022 to 2/15/2022 by week:

library(lubridate)

#generate sequence of dates from 1/1/2022 to 2/15/2022 by week
seq(ymd('2022-01-01'), ymd('2022-02-15'), by='1 week')

[1] "2022-01-01" "2022-01-08" "2022-01-15" "2022-01-22" "2022-01-29"
[6] "2022-02-05" "2022-02-12"

The result is a sequence of 7 dates ranging from 1/1/2022 to 2/25/2022 by week.

We could also use the following code to generate a sequence of dates from 1/1/2022 to 2/25/2022 by 2 week intervals:

library(lubridate)

#generate sequence of dates from 1/1/2022 to 2/15/2022 by 2 weeks
seq(ymd('2022-01-01'), ymd('2022-02-15'), by='2 week')

[1] "2022-01-01" "2022-01-15" "2022-01-29" "2022-02-12"

The result is a sequence of 4 dates ranging from 1/1/2022 to 2/25/2022 by 2 week intervals.

Example 3: Generate Sequence of Dates by Months

The following code shows how to generate a sequence of dates from 1/1/2022 to 10/31/2022 by month:

library(lubridate)

#generate sequence of dates from 1/1/2022 to 10/31/2022 by month
seq(ymd('2022-01-01'), ymd('2022-02-15'), by='1 month')

 [1] "2022-01-01" "2022-02-01" "2022-03-01" "2022-04-01" "2022-05-01"
 [6] "2022-06-01" "2022-07-01" "2022-08-01" "2022-09-01" "2022-10-01"

The result is a sequence of 10 dates ranging from 1/1/2022 to 2/10/31/2022 by month.

Note that in this tutorial we only shared a few examples of how to generate a sequence of dates using the lubridate package.

Feel free to modify the start date, end date, and interval to create the specific sequence of dates that you’d like.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Convert Date to Numeric in R
How to Extract Month from Date in R
How to Add and Subtract Months from a Date in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *