---
title: "working with dates"
author: "Nicholas Horton (nhorton@amherst.edu)"
date: "September 8, 2017"
output: 
  html_document:
    fig_height: 3
    fig_width: 6
  pdf_document:
    fig_height: 3
    fig_width: 5
  word_document:
    fig_height: 4
    fig_width: 6
---


```{r, include=FALSE}
# Don't delete this chunk if you are using the mosaic package
# This loads the mosaic and dplyr packages
require(mosaic)
```

```{r, include=FALSE}
# Some customization.  You can alter or delete as desired (if you know what you are doing).

# This changes the default colors in lattice plots.
trellis.par.set(theme=theme.mosaic())  

# knitr settings to control how R chunks work.
require(knitr)
opts_chunk$set(
  tidy=FALSE,     # display code as typed
  size="small"    # slightly smaller font for code
)
```

In the `Marriage` data set included in the `mosaicData` package, the `appdate`, `ceremonydate`, and `dob` variables are encoded as factors, even though they are dates. Use the `lubridate` package to convert those three columns into a date format.

```{r}
library(lubridate)
Marriage %>%
  mutate(app_date = mdy(appdate),
         ceremony_date = mdy(ceremonydate),
         date_of_birth = mdy(dob),
         date_of_birth = ifelse(year(date_of_birth) > year(now()),
                                date_of_birth - years(100),
                                date_of_birth),
         date_of_birth = as_date(date_of_birth)) %>%
glimpse()
```

