Dates and times

Author

Jeffrey R. Stevens

Published

March 22, 2023

For these exercises, we’ll use the dates data set.

  1. Load tidyverse, import dates.csv to dates, and view the data set.
library(tidyverse)
dates <- read_csv(here::here("data/dates.csv"), show_col_types = FALSE)
  1. Convert birth_date to a date object and resave dates.
dates <- dates |> 
  mutate(birth_date = mdy(birth_date))
  1. Create a column called time1 that converts test1 to datetime and change the time zone to “America/Chicago”.
dates |> 
  mutate(time1 = as_datetime(test1, tz = "America/Chicago"))
# A tibble: 20 × 5
      id birth_date test1      test2      time1              
   <dbl> <date>     <date>     <date>     <dttm>             
 1     1 1993-05-30 2022-01-27 2022-06-27 2022-01-27 00:00:00
 2     2 1962-02-05 2022-03-15 2022-09-14 2022-03-15 00:00:00
 3     3 1974-11-19 2022-01-17 2022-09-08 2022-01-17 00:00:00
 4     4 1995-06-14 2022-03-26 2022-09-07 2022-03-26 00:00:00
 5     5 1993-05-15 2022-02-13 2022-08-12 2022-02-13 00:00:00
 6     6 1955-04-08 2022-02-22 2022-08-08 2022-02-22 00:00:00
 7     7 1970-10-08 2022-02-17 2022-07-11 2022-02-17 00:00:00
 8     8 2000-02-07 2022-01-21 2022-08-05 2022-01-21 00:00:00
 9     9 1974-02-15 2022-03-04 2022-08-18 2022-03-04 00:00:00
10    10 1972-05-04 2022-02-01 2022-06-13 2022-02-01 00:00:00
11    11 1977-03-19 2022-03-26 2022-08-20 2022-03-26 00:00:00
12    12 1989-02-01 2022-01-18 2022-08-25 2022-01-18 00:00:00
13    13 2003-05-06 2022-02-04 2022-06-19 2022-02-04 00:00:00
14    14 1980-02-29 2022-02-18 2022-06-13 2022-02-18 00:00:00
15    15 1964-01-18 2022-01-18 2022-06-08 2022-01-18 00:00:00
16    16 1991-01-02 2022-01-04 2022-06-03 2022-01-04 00:00:00
17    17 2002-03-22 2022-01-12 2022-09-13 2022-01-12 00:00:00
18    18 1994-07-01 2022-03-15 2022-09-09 2022-03-15 00:00:00
19    19 2004-01-24 2022-03-09 2022-09-28 2022-03-09 00:00:00
20    20 1962-03-05 2022-01-11 2022-08-24 2022-01-11 00:00:00
  1. Calculate each participant’s age in years at the time of test 1, rounded to 1 decimal place, stored in age and resave dates.
dates <- dates |> 
  mutate(age = round(as.numeric(test1 - birth_date) / 365.25, 1))
  1. Calculate the number of days between test 1 and test 2 for each participant and label this column test_diff (and resave dates).
dates <- dates |> 
  mutate(test_diff = test2 - test1)
  1. Create dates2 that subsets the participants who were born after January 1, 1970.
dates2 <- dates |> 
  filter(birth_date > "1970-01-01")
  1. Create a column named diff_text that writes the following sentence for each participant in dates2: “Participant [insert id] (age: [insert age]) had test 1 on [insert test1] and test 2 on [insert test2], which were [insert test_diff] days apart.”
dates2 |> 
  mutate(diff_text = stringr::str_glue("Participant {id} (age: {age}) had test 1 on {test1} and test 2 on {test2}, which were {test_diff} days apart."))
# A tibble: 16 × 7
      id birth_date test1      test2        age test_diff diff_text             
   <dbl> <date>     <date>     <date>     <dbl> <drtn>    <glue>                
 1     1 1993-05-30 2022-01-27 2022-06-27  28.7 151 days  Participant 1 (age: 2…
 2     3 1974-11-19 2022-01-17 2022-09-08  47.2 234 days  Participant 3 (age: 4…
 3     4 1995-06-14 2022-03-26 2022-09-07  26.8 165 days  Participant 4 (age: 2…
 4     5 1993-05-15 2022-02-13 2022-08-12  28.8 180 days  Participant 5 (age: 2…
 5     7 1970-10-08 2022-02-17 2022-07-11  51.4 144 days  Participant 7 (age: 5…
 6     8 2000-02-07 2022-01-21 2022-08-05  22   196 days  Participant 8 (age: 2…
 7     9 1974-02-15 2022-03-04 2022-08-18  48   167 days  Participant 9 (age: 4…
 8    10 1972-05-04 2022-02-01 2022-06-13  49.7 132 days  Participant 10 (age: …
 9    11 1977-03-19 2022-03-26 2022-08-20  45   147 days  Participant 11 (age: …
10    12 1989-02-01 2022-01-18 2022-08-25  33   219 days  Participant 12 (age: …
11    13 2003-05-06 2022-02-04 2022-06-19  18.8 135 days  Participant 13 (age: …
12    14 1980-02-29 2022-02-18 2022-06-13  42   115 days  Participant 14 (age: …
13    16 1991-01-02 2022-01-04 2022-06-03  31   150 days  Participant 16 (age: …
14    17 2002-03-22 2022-01-12 2022-09-13  19.8 244 days  Participant 17 (age: …
15    18 1994-07-01 2022-03-15 2022-09-09  27.7 178 days  Participant 18 (age: …
16    19 2004-01-24 2022-03-09 2022-09-28  18.1 203 days  Participant 19 (age: …