Purpose
To explore lubridate package from Hadley Wickham
> library(lubridate)
> tdate <- dmy("01-02-2010")
> month(tdate)
[1] 2
> year(tdate)
[1] 2010
> tdate - 1
[1] "2010-01-31 23:59:59 UTC" |
Get the system date using lubridate, Date and POSIXct
> now()
[1] "2011-07-28 00:07:26 IST"
> Sys.Date()
[1] "2011-07-28"
> Sys.time()
[1] "2011-07-28 00:07:26 IST" |
Origin
> origin + days(1000)
[1] "1972-09-27 UTC"
> tdate + days(1)
[1] "2010-02-02 UTC"
> tdate - days(1)
[1] "2010-01-31 UTC"
> wday(tdate)
[1] 2
> yday(tdate)
[1] 32
> decimal_date(tdate)
[1] 2010.085
> tdate
[1] "2010-02-01 UTC"
> format(tdate, "%j")
[1] "032"
> round_date(tdate, "month")
[1] "2010-02-01 UTC"
> ymd(format(tdate, "%Y-%m-%d"))
[1] "2010-02-01 UTC" |
> rawfile.f.name <- "C:/Cauldron/garage/R/Data/Pairs/Pairs_Prices_Model_July7.csv"
> rawdata <- read.csv(rawfile.f.name, header = T, stringsAsFactors = F)
> rawdata <- rawdata[, -1]
> colnames(rawdata) <- c("ticker", "date", "high", "low", "close")
> rawdata$trade.date <- dmy(rawdata$date) |
> library(lattice)
> histogram(wday(rawdata$trade.date))
> xtabs(month(rawdata$trade.date) ~ wday(rawdata$trade.date))
wday(rawdata$trade.date)
2 3 4 5 6 7
79337 83214 81042 83053 75623 310
> xtabs(year(rawdata$trade.date) ~ month(rawdata$trade.date))
month(rawdata$trade.date)
1 2 3 4 5 6 7 8
12113330 12424880 13355818 11805492 13355818 13667368 8092876 6854100
9 10 11 12
6542550 6542550 12742310 13349175
> xtabs(~wday(rawdata$trade.date) + month(rawdata$trade.date))
month(rawdata$trade.date)
wday(rawdata$trade.date) 1 2 3 4 5 6 7 8 9 10 11
2 1390 1236 1236 1236 1545 1236 774 775 620 620 1392
3 1081 1236 1545 1082 1390 1391 774 775 620 620 1392
4 1082 1236 1236 1081 1236 1545 774 620 775 620 1083
5 1236 1236 1390 1237 1236 1390 775 620 775 620 1237
6 1236 1081 1236 1236 1236 1236 929 620 465 775 1237
7 0 155 0 0 0 0 0 0 0 0 0
month(rawdata$trade.date)
wday(rawdata$trade.date) 12
2 1081
3 1391
4 1545
5 1545
6 1081
7 0 |
> tdate + hours(3)
[1] "2010-02-01 03:00:00 UTC"
> hour(tdate)
[1] 0
> day(tdate) - 1
[1] 0
> is.instant(now())
[1] TRUE |
> start_2011 <- ymd_hms("2011-01-01 12:00:00")
> start_2010 <- ymd_hms("2010-01-01 12:00:00")
> span <- start_2011 - start_2010
> start_2010 + (span)
[1] "2011-01-01 12:00:00 UTC"
> start_2011 + (span)
[1] "2012-01-01 12:00:00 UTC"
> start_2011 + as.duration(span)
[1] "2012-01-01 12:00:00 UTC"
> start_2011 + ddays(6)
[1] "2011-01-07 12:00:00 UTC" |
> head(rawdata)
ticker date high low close trade.date
1 A B B 04/11/2009 735.00 706.05 725.05 2009-11-04
2 A B B 03/11/2009 759.90 727.10 731.60 2009-11-03
3 A B B 03/12/2009 742.85 730.00 734.50 2009-12-03
4 A B B 07/12/2009 744.90 731.40 734.95 2009-12-07
5 A B B 04/12/2009 739.70 728.05 735.35 2009-12-04
6 A B B 02/12/2009 746.00 735.00 736.95 2009-12-02
> temp <- rawdata[, c("trade.date", "close")]
> head(temp)
trade.date close
1 2009-11-04 725.05
2 2009-11-03 731.60
3 2009-12-03 734.50
4 2009-12-07 734.95
5 2009-12-04 735.35
6 2009-12-02 736.95 |
> temp$trade.date.new <- temp$trade.date + days(5)
> head(temp)
trade.date close trade.date.new
1 2009-11-04 725.05 2009-11-09
2 2009-11-03 731.60 2009-11-08
3 2009-12-03 734.50 2009-12-08
4 2009-12-07 734.95 2009-12-12
5 2009-12-04 735.35 2009-12-09
6 2009-12-02 736.95 2009-12-07 |