introd u ction to dates
play

Introd u ction to dates W OR K IN G W ITH DATE S AN D TIME S IN - PowerPoint PPT Presentation

Introd u ction to dates W OR K IN G W ITH DATE S AN D TIME S IN R Charlo e Wickham Instr u ctor Dates Di erent con v entions in di erent places 27 th Feb 2013 NZ : 27/2/2013 USA : 2/27/2013 WORKING WITH DATES AND TIMES IN R The


  1. Introd u ction to dates W OR K IN G W ITH DATE S AN D TIME S IN R Charlo � e Wickham Instr u ctor

  2. Dates Di � erent con v entions in di � erent places 27 th Feb 2013 NZ : 27/2/2013 USA : 2/27/2013 WORKING WITH DATES AND TIMES IN R

  3. The global standard n u meric date format 1 h � ps ://x kcd . com /1179/ WORKING WITH DATES AND TIMES IN R

  4. ISO 8601 YYYY - MM - DD Val u es ordered from the largest to smallest u nit of time Each has a �x ed n u mber of digits , m u st be padded w ith leading z eros Either , no separators for comp u ters , or - in dates 1 st of Jan u ar y 2011 -> 2011-01-01 WORKING WITH DATES AND TIMES IN R

  5. Dates in R 2003-02-27 as.Date("2003-02-27") 1974 "2003-02-27" "2003-02-27" str(as.Date("2003-02-27")) "2003-02-27" Date[1:1], format: "2003-02-27" str("2003-02-27") Packages that import dates : readr , anytime chr "2003-02-27" WORKING WITH DATES AND TIMES IN R

  6. Let ' s practice ! W OR K IN G W ITH DATE S AN D TIME S IN R

  7. Wh y u se dates ? W OR K IN G W ITH DATE S AN D TIME S IN R Charlo � e Wickham Instr u ctor

  8. Dates act like n u mbers Date objects are stored as da y s 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

  9. Plotting w ith 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

  10. Plotting w ith 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

  11. 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

  12. Let ' s practice ! W OR K IN G W ITH DATE S AN D TIME S IN R

  13. What abo u t times ? W OR K IN G W ITH DATE S AN D TIME S IN R Charlo � e Wickham Instr u ctor

  14. ISO 8601 HH : MM : SS Largest u nit to smallest Fi x ed digits Ho u rs : 00 -- 24 Min u tes : 00 -- 59 Seconds : 00 -- 60 (60 onl y for leap seconds ) No separator or : WORKING WITH DATES AND TIMES IN R

  15. Datetimes in R T w o objects t y pes : POSIXlt - list w ith named components POSIXct - seconds since 1970-01-01 00:00:00 POSIXct w ill go in a data frame as.POSIXct() t u rns a string into a POSIXct object x <- as.POSIXct("1970-01-01 00:01:00") str(x) POSIXct[1:1], format: "1970-01-01 00:01:00 WORKING WITH DATES AND TIMES IN R

  16. Time z ones "2013-02-27T18:00:00" - 6 pm local time "2013-02-27T18:00:00Z" - 6 pm UTC "2013-02-27T18:00:00-08:00" - 6 pm in Oregon as.POSIXct("2013-02-27T18:00:00Z") "2013-02-27 PST" as.POSIXct("2013-02-27T18:00:00Z", tz = "UTC") "2013-02-27 UTC" WORKING WITH DATES AND TIMES IN R

  17. Datetimes beha v e nicel y too Once a POSIXct object , datetimes can be : Compared S u btracted Plo � ed WORKING WITH DATES AND TIMES IN R

  18. Let ' s practice ! W OR K IN G W ITH DATE S AN D TIME S IN R

  19. Wh y l u bridate ? W OR K IN G W ITH DATE S AN D TIME S IN R Charlo � e Wickham Instr u ctor

  20. l u bridate Make w orking w ith dates and times in R eas y! tidyverse package Pla y s nicel y w ith b u iltin datetime objects Designed for h u mans not comp u ters Pla y s nicel y w ith other tidyverse packages Consistent beha v io u r regardless of u nderl y ing object WORKING WITH DATES AND TIMES IN R

  21. Parsing a w ide range of formats ymd("2013-02-27") "2013-02-27" dmy("27/2/13") "2013-02-27" parse_date_time(c("Feb 27th, 2017", "27th Feb 2017"), + order = c("mdy", "dmy")) "2017-02-27 UTC" "2017-02-27 UTC" WORKING WITH DATES AND TIMES IN R

  22. Manip u lating datetimes # Extract components akl_daily <- akl_daily %>% mutate( year = year(date), yday = yday(date), month = month(date, label = TRUE) ) WORKING WITH DATES AND TIMES IN R

  23. Time spans # A tibble: 131 x 3 name period <chr> <S4: Period> 1 Elizabeth II 65y 7m 27d 0H 0M 0S 2 Victoria 63y 7m 2d 0H 0M 0S 3 George V 25y 8m 14d 0H 0M 0S 4 George III 19y 0m 28d 0H 0M 0S 5 George VI 15y 1m 26d 0H 0M 0S WORKING WITH DATES AND TIMES IN R

  24. Other l u bridate feat u res Handling time z ones Fast parsing of standard formats O u tp u � ing datetimes WORKING WITH DATES AND TIMES IN R

  25. Let ' s practice ! W OR K IN G W ITH DATE S AN D TIME S IN R

Recommend


More recommend