diff --git a/datetimes.Rmd b/datetimes.Rmd index d9fb639..6dab817 100644 --- a/datetimes.Rmd +++ b/datetimes.Rmd @@ -2,13 +2,21 @@ ## Introduction -This chapter will show you how to work with dates and times in R. Dates and times follow their own rules, which can make working with them difficult. For example dates and times are ordered, like numbers; but the timeline is not as orderly as the number line. The timeline repeats itself, and has noticeable gaps due to Daylight Savings Time, leap years, and leap seconds. Date-times also rely on ambiguous units: How long is a month? How long is a year? Time zones give you another headache when you work with dates and times. The same instant of time will have different "names" in different time zones. +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: + +* 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. + +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. ### Prerequisites -This chapter will focus on R's __lubridate__ package, which makes it much easier to work with dates and times in R. You'll learn the basic date-time structures in R and the lubridate functions that make working with them easy. We will use `nycflights13` for practice data, and use some packages for EDA. +This chapter will focus on the __lubridate__ package, which makes it easier to work with dates and times in R. We will use nycflights13 for practice data, and some packages for EDA. -```{r message = FALSE} +```{r setup, message = FALSE} library(lubridate) library(nycflights13) @@ -16,43 +24,41 @@ library(dplyr) library(ggplot2) ``` -## Parsing times +## Creating date/times -Time data normally comes as character strings, or numbers spread across columns, as in the `flights` dataset from [Relational data]. +There are three important + +* A __date__. Number of days since Jan 1, 1970. `` + +* 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__, the number of seconds. A date + a time = a date-time. Not + discussed furher in this chapter. `