diff --git a/02 Dates and times.Rmd b/02 Dates and times.Rmd index 0e141d7..190ab60 100644 --- a/02 Dates and times.Rmd +++ b/02 Dates and times.Rmd @@ -113,6 +113,12 @@ b-mdy("12/01/2014") date(now())-b ``` +When subtracting dates, R will make a good guess for the unit of time to use in the result. Use `difftime()` if you want to be specific about the unit of time and not leave it up to R to decide. +```{r comment="", results='hold'} +difftime(b, mdy("12/01/2014"), units = "days") +difftime(b, mdy("12/01/2014"), units = "hours") +``` + We can add time to the dates as well ```{r comment="", results='hold'} b + dyears(1) # adds 365 days, does not increase year by 1 @@ -175,7 +181,7 @@ a[min(which(tzone=="CST"))] 2. On what date will Thanksgiving fall in 2020? ```{r comment="", results='hold'} a <- mdy(paste0("11/",1:30,"/2020")) -a[wday(a,label=TRUE)=="Thurs"][4] +a[wday(a,label=TRUE)=="Thu"][4] ``` 3. Make a function that takes as input a year and returns the date of Thanksgiving in that year. @@ -183,7 +189,7 @@ a[wday(a,label=TRUE)=="Thurs"][4] tday <- function(year) { a <- mdy(paste0("11/",1:30,"/",year)) - return( a[wday(a,label=TRUE)=="Thurs"][4] ) + return( a[wday(a,label=TRUE)=="Thu"][4] ) } tday(2020) lapply(2020:2100, tday) diff --git a/02_Dates_and_times.html b/02_Dates_and_times.html index a9cc317..cfde09e 100644 --- a/02_Dates_and_times.html +++ b/02_Dates_and_times.html @@ -10,7 +10,7 @@ - + Dates and Times @@ -92,6 +92,9 @@ .tabbed-pane { padding-top: 12px; } +.html-widget { + margin-bottom: 20px; +} button.code-folding-btn:focus { outline: none; } @@ -122,13 +125,13 @@

Dates and Times

Greg Ridgeway

-University of Pennsylvania
gridge@upenn.edu +University of Pennsylvania
gridge@sas.upenn.edu

Ruth Moyer

-University of Pennsylvania
moyruth@upenn.edu +University of Pennsylvania
moyruth@sas.upenn.edu
-

July 26, 2018

+

September 18, 2018

@@ -140,7 +143,7 @@

July 26, 2018

- + @@ -148,6 +151,7 @@

July 26, 2018

+

Introduction

Working with dates and times is a lot different than working with the more familiar numbers. Months have different number of days. Sometimes we count hours of the day up to 12 and then start over. Sometimes we count hours up to 24 and then start over. Some years have 366 days. We have 24 times zones around the world. Twice a year we switch clocks for Daylight Saving Time, except in some places like Arizona. Arithmetic, such as adding one month to a date, is poorly defined. Which date is one month after January 31st? Is it February 28th? Or is it March 3rd?

@@ -204,7 +208,14 @@

Working with dates

Time differences in days
 [1]  8  4  1 -3 -7
 Time differences in days
-[1] 1325 1329 1332 1336 1340
+[1] 1379 1383 1386 1390 1394 +

When subtracting dates, R will make a good guess for the unit of time to use in the result. Use difftime() if you want to be specific about the unit of time and not leave it up to R to decide.

+
difftime(b, mdy("12/01/2014"), units = "days")
+difftime(b, mdy("12/01/2014"), units = "hours")
+
Time differences in days
+[1]  8  4  1 -3 -7
+Time differences in hours
+[1]  192   96   24  -72 -168

We can add time to the dates as well

b + dyears(1) # adds 365 days, does not increase year by 1
 b + ddays(31)
@@ -374,261 +385,261 @@

Solutions to the exercises

  • On what date will Thanksgiving fall in 2020?
  • a <- mdy(paste0("11/",1:30,"/2020"))
    -a[wday(a,label=TRUE)=="Thurs"][4]
    -
    [1] NA
    +a[wday(a,label=TRUE)=="Thu"][4] +
    [1] "2020-11-26"
    1. Make a function that takes as input a year and returns the date of Thanksgiving in that year.
    tday <- function(year)
     {
        a <- mdy(paste0("11/",1:30,"/",year))
    -   return( a[wday(a,label=TRUE)=="Thurs"][4] )
    +   return( a[wday(a,label=TRUE)=="Thu"][4] )
     }
     tday(2020)
     lapply(2020:2100, tday)
    -
    [1] NA
    +
    [1] "2020-11-26"
     [[1]]
    -[1] NA
    +[1] "2020-11-26"
     
     [[2]]
    -[1] NA
    +[1] "2021-11-25"
     
     [[3]]
    -[1] NA
    +[1] "2022-11-24"
     
     [[4]]
    -[1] NA
    +[1] "2023-11-23"
     
     [[5]]
    -[1] NA
    +[1] "2024-11-28"
     
     [[6]]
    -[1] NA
    +[1] "2025-11-27"
     
     [[7]]
    -[1] NA
    +[1] "2026-11-26"
     
     [[8]]
    -[1] NA
    +[1] "2027-11-25"
     
     [[9]]
    -[1] NA
    +[1] "2028-11-23"
     
     [[10]]
    -[1] NA
    +[1] "2029-11-22"
     
     [[11]]
    -[1] NA
    +[1] "2030-11-28"
     
     [[12]]
    -[1] NA
    +[1] "2031-11-27"
     
     [[13]]
    -[1] NA
    +[1] "2032-11-25"
     
     [[14]]
    -[1] NA
    +[1] "2033-11-24"
     
     [[15]]
    -[1] NA
    +[1] "2034-11-23"
     
     [[16]]
    -[1] NA
    +[1] "2035-11-22"
     
     [[17]]
    -[1] NA
    +[1] "2036-11-27"
     
     [[18]]
    -[1] NA
    +[1] "2037-11-26"
     
     [[19]]
    -[1] NA
    +[1] "2038-11-25"
     
     [[20]]
    -[1] NA
    +[1] "2039-11-24"
     
     [[21]]
    -[1] NA
    +[1] "2040-11-22"
     
     [[22]]
    -[1] NA
    +[1] "2041-11-28"
     
     [[23]]
    -[1] NA
    +[1] "2042-11-27"
     
     [[24]]
    -[1] NA
    +[1] "2043-11-26"
     
     [[25]]
    -[1] NA
    +[1] "2044-11-24"
     
     [[26]]
    -[1] NA
    +[1] "2045-11-23"
     
     [[27]]
    -[1] NA
    +[1] "2046-11-22"
     
     [[28]]
    -[1] NA
    +[1] "2047-11-28"
     
     [[29]]
    -[1] NA
    +[1] "2048-11-26"
     
     [[30]]
    -[1] NA
    +[1] "2049-11-25"
     
     [[31]]
    -[1] NA
    +[1] "2050-11-24"
     
     [[32]]
    -[1] NA
    +[1] "2051-11-23"
     
     [[33]]
    -[1] NA
    +[1] "2052-11-28"
     
     [[34]]
    -[1] NA
    +[1] "2053-11-27"
     
     [[35]]
    -[1] NA
    +[1] "2054-11-26"
     
     [[36]]
    -[1] NA
    +[1] "2055-11-25"
     
     [[37]]
    -[1] NA
    +[1] "2056-11-23"
     
     [[38]]
    -[1] NA
    +[1] "2057-11-22"
     
     [[39]]
    -[1] NA
    +[1] "2058-11-28"
     
     [[40]]
    -[1] NA
    +[1] "2059-11-27"
     
     [[41]]
    -[1] NA
    +[1] "2060-11-25"
     
     [[42]]
    -[1] NA
    +[1] "2061-11-24"
     
     [[43]]
    -[1] NA
    +[1] "2062-11-23"
     
     [[44]]
    -[1] NA
    +[1] "2063-11-22"
     
     [[45]]
    -[1] NA
    +[1] "2064-11-27"
     
     [[46]]
    -[1] NA
    +[1] "2065-11-26"
     
     [[47]]
    -[1] NA
    +[1] "2066-11-25"
     
     [[48]]
    -[1] NA
    +[1] "2067-11-24"
     
     [[49]]
    -[1] NA
    +[1] "2068-11-22"
     
     [[50]]
    -[1] NA
    +[1] "2069-11-28"
     
     [[51]]
    -[1] NA
    +[1] "2070-11-27"
     
     [[52]]
    -[1] NA
    +[1] "2071-11-26"
     
     [[53]]
    -[1] NA
    +[1] "2072-11-24"
     
     [[54]]
    -[1] NA
    +[1] "2073-11-23"
     
     [[55]]
    -[1] NA
    +[1] "2074-11-22"
     
     [[56]]
    -[1] NA
    +[1] "2075-11-28"
     
     [[57]]
    -[1] NA
    +[1] "2076-11-26"
     
     [[58]]
    -[1] NA
    +[1] "2077-11-25"
     
     [[59]]
    -[1] NA
    +[1] "2078-11-24"
     
     [[60]]
    -[1] NA
    +[1] "2079-11-23"
     
     [[61]]
    -[1] NA
    +[1] "2080-11-28"
     
     [[62]]
    -[1] NA
    +[1] "2081-11-27"
     
     [[63]]
    -[1] NA
    +[1] "2082-11-26"
     
     [[64]]
    -[1] NA
    +[1] "2083-11-25"
     
     [[65]]
    -[1] NA
    +[1] "2084-11-23"
     
     [[66]]
    -[1] NA
    +[1] "2085-11-22"
     
     [[67]]
    -[1] NA
    +[1] "2086-11-28"
     
     [[68]]
    -[1] NA
    +[1] "2087-11-27"
     
     [[69]]
    -[1] NA
    +[1] "2088-11-25"
     
     [[70]]
    -[1] NA
    +[1] "2089-11-24"
     
     [[71]]
    -[1] NA
    +[1] "2090-11-23"
     
     [[72]]
    -[1] NA
    +[1] "2091-11-22"
     
     [[73]]
    -[1] NA
    +[1] "2092-11-27"
     
     [[74]]
    -[1] NA
    +[1] "2093-11-26"
     
     [[75]]
    -[1] NA
    +[1] "2094-11-25"
     
     [[76]]
    -[1] NA
    +[1] "2095-11-24"
     
     [[77]]
    -[1] NA
    +[1] "2096-11-22"
     
     [[78]]
    -[1] NA
    +[1] "2097-11-28"
     
     [[79]]
    -[1] NA
    +[1] "2098-11-27"
     
     [[80]]
    -[1] NA
    +[1] "2099-11-26"
     
     [[81]]
    -[1] NA
    +[1] "2100-11-25"