我每天都有一系列日期。现在我需要每月第一天和每个月最后一天的指数。我不知道如何实现这一点。
# Just a simple datesequence
dateSeq <- seq(from=as.Date("2013-01-01"), to=as.Date("2017-01-01"), by="day")
我需要一个载体,其中所有指数都是月的第一天(1,32,...)和月的最后一天(31,68,......)
编辑:我也需要一周,一年,一学期,两个半月的第一天和最后一天,所以应该有一致的方式来做这件事如果日期序列在2017年3月12日结束,例如我不需要这一天在索引中,因为它不是该月的最后一天,而是序列中该月的最后一天。
答案 0 :(得分:1)
以下是月份和工作日的第一天和最后一天的指数
库(lubridate)
b=unique(floor_date(dateSeq, "month"))
> match(b,dateSeq)
[1] 1 32 60 91 121 152 182 213 244 274 305 335 366 397 425 456 486 517 547 578 609 639 670 700 731 762 790 821 851 882 912 943
[33] 974 1004 1035 1065 1096 1127 1156 1187 1217 1248 1278 1309 1340 1370 1401 1431 1462
> c=unique(floor_date(dateSeq, "month"))-1
> match(c,dateSeq)
[1] NA 31 59 90 120 151 181 212 243 273 304 334 365 396 424 455 485 516 546 577 608 638 669 699 730 761 789 820 850 881 911 942
[33] 973 1003 1034 1064 1095 1126 1155 1186 1216 1247 1277 1308 1339 1369 1400 1430 1461
每周第一天的周日
> unique(floor_date(dateSeq, "month"))
[1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01" "2013-12-01"
[13] "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01" "2014-09-01" "2014-10-01" "2014-11-01" "2014-12-01"
[25] "2015-01-01" "2015-02-01" "2015-03-01" "2015-04-01" "2015-05-01" "2015-06-01" "2015-07-01" "2015-08-01" "2015-09-01" "2015-10-01" "2015-11-01" "2015-12-01"
[37] "2016-01-01" "2016-02-01" "2016-03-01" "2016-04-01" "2016-05-01" "2016-06-01" "2016-07-01" "2016-08-01" "2016-09-01" "2016-10-01" "2016-11-01" "2016-12-01"
[49] "2017-01-01"
> wday(unique(floor_date(dateSeq, "month")))
[1] 3 6 6 2 4 7 2 5 1 3 6 1 4 7 7 3 5 1 3 6 2 4 7 2 5 1 1 4 6 2 4 7 3 5 1 3 6 2 3 6 1 4 6 2 5 7 3 5 1
> weekdays(unique(floor_date(dateSeq, "month")))
[1] "Tuesday" "Friday" "Friday" "Monday" "Wednesday" "Saturday" "Monday" "Thursday" "Sunday" "Tuesday" "Friday" "Sunday" "Wednesday"
[14] "Saturday" "Saturday" "Tuesday" "Thursday" "Sunday" "Tuesday" "Friday" "Monday" "Wednesday" "Saturday" "Monday" "Thursday" "Sunday"
[27] "Sunday" "Wednesday" "Friday" "Monday" "Wednesday" "Saturday" "Tuesday" "Thursday" "Sunday" "Tuesday" "Friday" "Monday" "Tuesday"
[40] "Friday" "Sunday" "Wednesday" "Friday" "Monday" "Thursday" "Saturday" "Tuesday" "Thursday" "Sunday"
最后一个月的Weekday
> unique(floor_date(dateSeq, "month"))-1
[1] "2012-12-31" "2013-01-31" "2013-02-28" "2013-03-31" "2013-04-30" "2013-05-31" "2013-06-30" "2013-07-31" "2013-08-31" "2013-09-30" "2013-10-31" "2013-11-30"
[13] "2013-12-31" "2014-01-31" "2014-02-28" "2014-03-31" "2014-04-30" "2014-05-31" "2014-06-30" "2014-07-31" "2014-08-31" "2014-09-30" "2014-10-31" "2014-11-30"
[25] "2014-12-31" "2015-01-31" "2015-02-28" "2015-03-31" "2015-04-30" "2015-05-31" "2015-06-30" "2015-07-31" "2015-08-31" "2015-09-30" "2015-10-31" "2015-11-30"
[37] "2015-12-31" "2016-01-31" "2016-02-29" "2016-03-31" "2016-04-30" "2016-05-31" "2016-06-30" "2016-07-31" "2016-08-31" "2016-09-30" "2016-10-31" "2016-11-30"
[49] "2016-12-31"
> wday(unique(floor_date(dateSeq, "month"))-1)
[1] 2 5 5 1 3 6 1 4 7 2 5 7 3 6 6 2 4 7 2 5 1 3 6 1 4 7 7 3 5 1 3 6 2 4 7 2 5 1 2 5 7 3 5 1 4 6 2 4 7
> weekdays(unique(floor_date(dateSeq, "month"))-1)
[1] "Monday" "Thursday" "Thursday" "Sunday" "Tuesday" "Friday" "Sunday" "Wednesday" "Saturday" "Monday" "Thursday" "Saturday" "Tuesday"
[14] "Friday" "Friday" "Monday" "Wednesday" "Saturday" "Monday" "Thursday" "Sunday" "Tuesday" "Friday" "Sunday" "Wednesday" "Saturday"
[27] "Saturday" "Tuesday" "Thursday" "Sunday" "Tuesday" "Friday" "Monday" "Wednesday" "Saturday" "Monday" "Thursday" "Sunday" "Monday"
[40] "Thursday" "Saturday" "Tuesday" "Thursday" "Sunday" "Wednesday" "Friday" "Monday" "Wednesday" "Saturday"