Why use dates?

Working with Dates and Times in R

Charlotte Wickham

Instructor

Dates act like numbers

Date objects are stored as days since 1970-01-01

as.Date("2003-02-27") > as.Date("2002-02-27")
TRUE
as.Date("2003-02-27") + 1
"2003-02-28"
as.Date("2003-02-27") - as.Date("2002-02-27")
Time difference of 365 days
Working with Dates and Times in R

Plotting with dates

x <- c(as.Date("2003-02-27"), 
       as.Date("2003-03-27"), 
       as.Date("2003-04-27"))
plot(x, 1:3)

Working with Dates and Times in R

Plotting with dates

x <- c(as.Date("2003-02-27"), 
       as.Date("2003-03-27"), 
       as.Date("2003-04-27"))

library(ggplot2)
ggplot() + 
  geom_point(aes(x = x, y = 1:3))

Working with Dates and Times in R

R releases

releases
# A tibble: 105 x 7
   major minor patch       date            datetime     time  type
   <int> <int> <int>     <date>              <dttm>   <time> <chr>
 1     0    60    NA 1997-12-04 1997-12-04 08:47:58 08:47:58 patch
 2     0    61    NA 1997-12-21 1997-12-21 13:09:22 13:09:22 minor
 3     0    61     1 1998-01-10 1998-01-10 00:31:55 00:31:55 patch
 4     0    61     2 1998-03-14 1998-03-14 19:25:55 19:25:55 patch
 5     0    61     3 1998-05-02 1998-05-02 07:58:17 07:58:17 patch
 6     0    62    NA 1998-06-14 1998-06-14 12:56:20 12:56:20 minor
 7     0    62     1 1998-06-14 1998-06-14 22:13:25 22:13:25 patch
 8     0    62     2 1998-07-10 1998-07-10 11:13:45 11:13:45 patch
 9     0    62     3 1998-08-28 1998-08-28 09:02:19 09:02:19 patch
10     0    62     4 1998-10-23 1998-10-23 12:08:41 12:08:41 patch
# ... with 95 more rows
Working with Dates and Times in R

Let's practice!

Working with Dates and Times in R

Preparing Video For Download...