How to generate a sequence of a date in each month for a fixed number of months using R?


Every month have common dates except few such as February do not have 30 or 31 and even 29 in some years and there are months that contain 30 days while some contains 31 days. Therefore, finding a date say the first date, a middle date, or a last date is not an easy task but it can be done with the help of seq function in base R.

Examples

 Live Demo

> seq(as.Date("2020-01-01"),length=12,by="1 month")

Output

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

Example

 Live Demo

> seq(as.Date("2020-01-01"),length=36,by="1 month")

Output

[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01"
[6] "2020-06-01" "2020-07-01" "2020-08-01" "2020-09-01" "2020-10-01"
[11] "2020-11-01" "2020-12-01" "2021-01-01" "2021-02-01" "2021-03-01"
[16] "2021-04-01" "2021-05-01" "2021-06-01" "2021-07-01" "2021-08-01"
[21] "2021-09-01" "2021-10-01" "2021-11-01" "2021-12-01" "2022-01-01"
[26] "2022-02-01" "2022-03-01" "2022-04-01" "2022-05-01" "2022-06-01"
[31] "2022-07-01" "2022-08-01" "2022-09-01" "2022-10-01" "2022-11-01"
[36] "2022-12-01"

Example

 Live Demo

> seq(as.Date("2020-01-04"),length=24,by="1 month")

Output

[1] "2020-01-04" "2020-02-04" "2020-03-04" "2020-04-04" "2020-05-04"
[6] "2020-06-04" "2020-07-04" "2020-08-04" "2020-09-04" "2020-10-04"
[11] "2020-11-04" "2020-12-04" "2021-01-04" "2021-02-04" "2021-03-04"
[16] "2021-04-04" "2021-05-04" "2021-06-04" "2021-07-04" "2021-08-04"
[21] "2021-09-04" "2021-10-04" "2021-11-04" "2021-12-04"

Example

 Live Demo

> seq(as.Date("2020-01-28"),length=24,by="2 month")

Output

[1] "2020-01-28" "2020-03-28" "2020-05-28" "2020-07-28" "2020-09-28"
[6] "2020-11-28" "2021-01-28" "2021-03-28" "2021-05-28" "2021-07-28"
[11] "2021-09-28" "2021-11-28" "2022-01-28" "2022-03-28" "2022-05-28"
[16] "2022-07-28" "2022-09-28" "2022-11-28" "2023-01-28" "2023-03-28"
[21] "2023-05-28" "2023-07-28" "2023-09-28" "2023-11-28"

Example

 Live Demo

> seq(as.Date("2020-01-01"),length=24,by="6 month")

Output

[1] "2020-01-01" "2020-07-01" "2021-01-01" "2021-07-01" "2022-01-01"
[6] "2022-07-01" "2023-01-01" "2023-07-01" "2024-01-01" "2024-07-01"
[11] "2025-01-01" "2025-07-01" "2026-01-01" "2026-07-01" "2027-01-01"
[16] "2027-07-01" "2028-01-01" "2028-07-01" "2029-01-01" "2029-07-01"
[21] "2030-01-01" "2030-07-01" "2031-01-01" "2031-07-01"

Example

 Live Demo

> seq(as.Date("2020-01-29"),length=24,by="1 month")

Output

[1] "2020-01-29" "2020-02-29" "2020-03-29" "2020-04-29" "2020-05-29"
[6] "2020-06-29" "2020-07-29" "2020-08-29" "2020-09-29" "2020-10-29"
[11] "2020-11-29" "2020-12-29" "2021-01-29" "2021-03-01" "2021-03-29"
[16] "2021-04-29" "2021-05-29" "2021-06-29" "2021-07-29" "2021-08-29"
[21] "2021-09-29" "2021-10-29" "2021-11-29" "2021-12-29"

Example

 Live Demo

> seq(as.Date("2020-02-01"),length=24,by="1 month")-1

Output

[1] "2020-01-31" "2020-02-29" "2020-03-31" "2020-04-30" "2020-05-31"
[6] "2020-06-30" "2020-07-31" "2020-08-31" "2020-09-30" "2020-10-31"
[11] "2020-11-30" "2020-12-31" "2021-01-31" "2021-02-28" "2021-03-31"
[16] "2021-04-30" "2021-05-31" "2021-06-30" "2021-07-31" "2021-08-31"
[21] "2021-09-30" "2021-10-31" "2021-11-30" "2021-12-31"

Updated on: 04-Sep-2020

675 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements