每个月都有共同的日期,但很少,例如2月在某些年份中没有30或31,甚至29,并且有些月份包含30天,有些月份包含31天。因此,查找日期,例如第一个日期,中间日期或最后一个日期不是一件容易的事,但是可以借助base 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"