This report is automatically generated with the R package knitr (version 1.5) .

# Chapter 6 - Going on a Date with R Working with Dates
xd <- as.Date("2012-07-27")
xd
## [1] "2012-07-27"
str(xd)
##  Date[1:1], format: "2012-07-27"
weekdays(xd)
## [1] "Friday"
xd + 7
## [1] "2012-08-03"
xd + 0:6
## [1] "2012-07-27" "2012-07-28" "2012-07-29" "2012-07-30" "2012-07-31" "2012-08-01"
## [7] "2012-08-02"
weekdays(xd + 0:6)
## [1] "Friday"    "Saturday"  "Sunday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"
startDate <- as.Date("2012-01-01")
## xm <- seq(startDate, by='2 months', length.out=6)
xm
## Error: object 'xm' not found
months(xm)
## Error: object 'xm' not found
quarters(xm)
## Error: object 'xm' not found
Sys.localeconv()
##     decimal_point     thousands_sep          grouping   int_curr_symbol   currency_symbol 
##               "."                ""                ""             "GBP"               "£" 
## mon_decimal_point mon_thousands_sep      mon_grouping     positive_sign     negative_sign 
##               "."               ","            "\003"                ""               "-" 
##   int_frac_digits       frac_digits     p_cs_precedes    p_sep_by_space     n_cs_precedes 
##               "2"               "2"               "1"               "0"               "1" 
##    n_sep_by_space       p_sign_posn       n_sign_posn 
##               "0"               "3"               "3"
as.Date("27 July 2012", format = "%d %B %Y")
## [1] "2012-07-27"
as.Date("27/7/12", format = "%d/%m/%y")
## [1] "2012-07-27"
# Adding Time Information to Dates
apollo <- "July 20, 1969, 20:17:39"
apollo.fmt <- "%B %d, %Y, %H:%M:%S"
xct <- as.POSIXct(apollo, format = apollo.fmt, tz = "UTC")
xct
## [1] "1969-07-20 20:17:39 UTC"
format(xct, "%d/%m/%y")
## [1] "20/07/69"
format(xct, "%S minutes past %I %p, on %d %B %Y")
## [1] "39 minutes past 08 PM, on 20 July 1969"
# Performing Operations on Dates and Times Addition and subtraction
24 * 60 * 60
## [1] 86400
xct + 7 * 86400
## [1] "1969-07-27 20:17:39 UTC"
xct + 3 * 60 * 60
## [1] "1969-07-20 23:17:39 UTC"
xct - 7 * 86400
## [1] "1969-07-13 20:17:39 UTC"
as.Date(xct) - 7
## [1] "1969-07-13"
## Comparison of dates
Sys.time()
## [1] "2014-05-13 15:05:36 BST"
Sys.time() < xct
## [1] FALSE
dec.start <- as.POSIXct("1950-01-01")
## dec <- seq(dec.start, by='10 years', length.out=4)
dec
## Error: object 'dec' not found
dec > xct
## Error: object 'dec' not found
## Extraction
xlt <- as.POSIXlt(xct)
xlt
## [1] "1969-07-20 20:17:39 UTC"
xlt$year
## [1] 69
xlt$mon
## [1] 6
unclass(xlt)
## $sec
## [1] 39
## 
## $min
## [1] 17
## 
## $hour
## [1] 20
## 
## $mday
## [1] 20
## 
## $mon
## [1] 6
## 
## $year
## [1] 69
## 
## $wday
## [1] 0
## 
## $yday
## [1] 200
## 
## $isdst
## [1] 0
## 
## attr(,"tzone")
## [1] "UTC"

The R session information (including the OS info, R version and all packages used):

sessionInfo()
## R version 3.0.2 (2013-09-25)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 
## locale:
## [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
## [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
## [5] LC_TIME=English_United Kingdom.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] BiocInstaller_1.12.1 ggplot2_0.9.3.1      reshape2_1.2.2       sos_1.3-8           
##  [5] brew_1.0-6           stringr_0.6.2        knitr_1.5            plyr_1.8            
##  [9] Revobase_7.1.0       RevoMods_7.1.0       RevoScaleR_7.1.0     lattice_0.20-27     
## [13] rpart_4.1-2         
## 
## loaded via a namespace (and not attached):
##  [1] codetools_0.2-8    colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4      
##  [5] evaluate_0.5.1     foreach_1.4.1      formatR_0.10       fortunes_1.5-2    
##  [9] grid_3.0.2         gtable_0.1.2       highr_0.3          iterators_1.0.6   
## [13] labeling_0.2       MASS_7.3-29        munsell_0.4.2      proto_0.3-10      
## [17] RColorBrewer_1.0-5 scales_0.2.3       tools_3.0.2
Sys.time()
## [1] "2014-05-13 15:05:37 BST"