2019年2月17日日曜日

day to day comparison 2018 vs. 2019 intersect merge



 use intersect() to calculate AND between index(bp.day["2019"]) and 2019 calendar.

> intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))
 [1] 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918
<skip>
[41] 17939 17940 17941 17942 17943 17944 17945
> length(seq(as.Date("1970-01-01"),as.Date("2019-01-01"),by='days'))
[1] 17898
> length(seq(as.Date("1970-01-01"),as.Date("1970-01-01"),by='days'))
[1] 1

resutls are given as the number of the days since "1970-01-01". therefore sbstract 17896 and 17896-365 will calculate the case for 2019 and 2018 respectively. intersecting between 2019 and 2018 will give back the date which exit both 2018 and 2019 in the daily sequential data.

>intersect(intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))-17896,intersect(index(bp.day["2018"]),seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days'))-(17896-365))
[1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
[39] 41 43 44 45 46 47

below will creates the day-to-day comparison graph between 2018 and 2019.


bp.day <- apply.daily(bp.bangkok,mean)
d <- intersect(intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))-17896,intersect(index(bp.day["2018"]),seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days'))-(17896-365))

bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]]
bp.day[seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]

merge(bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]],as.vector(bp.day[,1][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),as.vector(bp.day[,2][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),suffixes = c("","h18","h18"))


plot(merge(bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]],as.vector(bp.day[,1][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),as.vector(bp.day[,2][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),suffixes = c("","h18","h18")))





0 件のコメント: