如何使用 R 生成固定月份数内每个月的日期序列?


每个月都有共同的日期,除了少数月份,例如 2 月没有 30 或 31 号,甚至在某些年份没有 29 号,还有一些月份包含 30 天,而另一些包含 31 天。因此,查找某个日期,比如第一个日期、中间日期或最后一个日期,并非易事,但可以使用基本 R 中的 seq 函数来实现。

示例

 在线演示

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

输出

[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"

示例

 在线演示

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

输出

[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"

示例

 在线演示

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

输出

[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"

示例

 在线演示

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

输出

[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"

示例

 在线演示

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

输出

[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"

示例

 在线演示

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

输出

[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"

示例

 在线演示

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

输出

[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"

更新于: 2020-09-04

812 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告