From 0d8e7b55f059510bc088d12706040214efccc635 Mon Sep 17 00:00:00 2001 From: hadley Date: Thu, 28 Jul 2016 15:39:52 -0500 Subject: [PATCH] Date time tweaking --- datetimes.Rmd | 353 ++++++++++++++++-------------- diagrams/datetimes-arithmetic.png | Bin 0 -> 74859 bytes diagrams/datetimes.graffle | Bin 0 -> 4046 bytes 3 files changed, 193 insertions(+), 160 deletions(-) create mode 100644 diagrams/datetimes-arithmetic.png create mode 100644 diagrams/datetimes.graffle diff --git a/datetimes.Rmd b/datetimes.Rmd index 6dab817..28fe5a5 100644 --- a/datetimes.Rmd +++ b/datetimes.Rmd @@ -2,15 +2,15 @@ ## Introduction -This chapter will show you how to work with dates and times in R. At first glance, dates and times seem simple. You use them all the time in every day, and generally have too many problems. However, the more you learn about dates and times, the more complicated the get. For example: +This chapter will show you how to work with dates and times in R. At first glance, dates and times seem simple. You use them all the time in your regular life, and they don't seem to cause much confusion. However, the more you learn about dates and times, the more complicated they seem to get. For example: * Does every year have 365 days? * Does every day have 24 hours? * Does every minute have 60 seconds? -I'm sure you remembered that there are leap years that have 365 days (but do you know the full rule for determining if a year is a leap year?). You might have remembered that many parts of the world use daylight savings time, so that some days have 23 hours, and others have 25. You probably didn't know that some minutes have 61 seconds because occassionally leap seconds are added to keep things in synch. Read for even more things that you probably believe that are not true. +I'm sure you know that not every year has 365 days, but but do you know the full rule for determining if a year is a leap year? You might have remembered that many parts of the world use daylight savings time (DST), so that some days have 23 hours, and others have 25. You probably didn't know that some minutes have 61 seconds because every now and then leap seconds are added to keep because the Earth's rotation is gradually slowing down. -Dates and times are hard because they have to reconcile two physical phenonmen (the rotation of the Earth and its orbit around the sun) with a whole raft of cultural phenonmeon including months and time zones. This chapter won't teach you everything about dates and times, but it will give you a solid grounding of practical skills that will help you with common data analysis challenges. +Dates and times are hard because they have to reconcile two physical phenonmen (the rotation of the Earth and its orbit around the sun) with a whole raft of geopolitical phenonmeon including months, time zones, and DST. This chapter won't teach you every last detail about dates and times, but it will give you a solid grounding of practical skills that will help you with common data analysis challenges. ### Prerequisites @@ -19,51 +19,58 @@ This chapter will focus on the __lubridate__ package, which makes it easier to w ```{r setup, message = FALSE} library(lubridate) +# Data library(nycflights13) + +# EDA library(dplyr) library(ggplot2) ``` ## Creating date/times -There are three important +There three types of date/time data that refer to an instant in time: -* A __date__. Number of days since Jan 1, 1970. `` +* A __date__. Tibbles print this as ``. -* A __date-time__ is a date plus a time. POSIXct. (We'll come back to POSIXlt - later - but generally you should avoid it.). Number of seconds since Jan 1, 1970. - `` +* A __time__ within a day. Tibbles print this as `