Exercsie 12.6.1

Convert the following to date or date/time objects.

(a) September 13, 2010.

## [1] "2010-09-13"

(b) Sept 13, 2010.

## [1] "2010-09-13"

(c) Sep 13, 2010.

## [1] "2010-09-13"

(d) S 13, 2010. Comment on the month abbreviation needs.

mdy("S 13, 2010")
## [1] NA
date <- c("S 13, 2010")
date %>%
  str_replace("S", "Sep") %>%
  mdy()
## [1] "2010-09-13"

“S” does not seem to be enough information for lubridate.

(e) 07-Dec-1941.

## [1] "1941-12-07"

(f) 1-5-1998. Comment on why you might be wrong.

## [1] "1998-05-01"
## [1] "1998-01-05"

It isn’t clear which single-digit number represents the month, and which represents the day.

(g) 21-5-1998. Comment on why you know you are correct.

## [1] "1998-05-21"

There are not 21 months in a year, which means “21” must represent the day.

(h) 2020-May-5 10:30 am

## [1] "2020-05-05 10:30:00 UTC"

(i) 2020-May-5 10:30 am PDT (ex: Seattle)

## [1] "2020-05-05 10:30:00 PDT"

(j) 2020-May-5 10:30 am AST (ex: Puerto Rico)

## [1] "2020-05-05 10:30:00 AST"

Exercise 12.6.2

Using just your date of birth (ex: Sep 7, 1998) and today’s date, calculate the following. (Write your code in a manner that the code will work on any date after you were born.)

(a) Calculate the date of your 64th birthday.

## [1] "2059-06-09"

(b) Calculate your current age (in years). Hint: Check your age is calculated correctly if your birthday was yesterday and if it were tomorrow!

## [1] 26

(c) Using your result in part (b), calculate the date of your next birthday.

## [1] "2021-06-09"

(d) The number of days until your next birthday.

## [1] "105d 0H 0M 0S"

(e) The number of months and days until your next birthday.

## [1] "3m 16d 0H 0M 0S"

Exercise 12.6.3

Suppose you have arranged for a phone call to be at 3 pm on May 8, 2015 at Arizona time. However, the recipient will be in Auckland, NZ. What time will it be there?

## [1] "2015-05-09 10:00:00 NZST"

Exercise 12.6.4

From this book’s GitHub directory, navigate to the data-raw directory and then download the Pulliam_Airport_Weather_Station.csv data file. (There are several weather station files. Make sure you get the correct one!) There is a DATE column (is it of type date when you import the data?) as well as the Maximum and Minimum temperature. For the last 5 years of data we have (exactly, not just starting at Jan 1, 2014!), plot the time series of daily maximum temperature with date on the x-axis. Write your code so that it will work if I update the data set. Hint: Find the maximum date in the data set and then subtract 5 years. Will there be a difference if you use dyears(5) vs years(5)? Which seems more appropriate here?

Exercise 12.6.5

It turns out there is some interesting periodicity regarding the number of births on particular days of the year.

(a) Using the mosaicData package, load the data set Births78 which records the number of children born on each day in the United States in 1978. Because this problem is intended to show how to calculate the information using the date, remove all the columns except date and births.

data(Births78, package="mosaicData")

births <- Births78 %>%
  select(c("date", "births"))

(b) Graph the number of births vs the date with date on the x-axis. What stands out to you? Why do you think we have this trend?

Clearly there are some days that have many more births than other days, though it’s hard to determine exactly which days of the week follow which trend from this graph. I’m wondering if it has to do with the dates/times people schedule c-sections for, depending on what proportion of births are c-sections?

(c) To test your assumption, we need to figure out the what day of the week each observation is. Use dplyr::mutate to add a new column named dow that is the day of the week (Monday, Tuesday, etc). This calculation will involve some function in the lubridate package and the date column.

births <- births %>%
  mutate(dow = wday(date, label=TRUE))

(d) Plot the data with the point color being determined by the day of the week variable.

It does look like there are fewer births on the weekends, generally, which may have to do with hospital scheduling!