Using lubridate to Work with Dates

Wednesday April 29th

Today we will…

  • Midterm Exam - What to expect
  • New Material
    • Working with Date & Time Variables
  • PA 5.2: Jewel Heist

Midterm Info

Midterm Exam - Wed 5/6

  • This is a two-part exam completed in the one hour and 50 minute class period
    1. You will first complete a General Questions section on paper and without your computer.
    2. After you turn that in, you will complete a Short Answer section with your computer.

Midterm Exam - Wed 5/6

  • Review the “What to Expect” document thoroughly as it includes
    • detailed expectations
    • the dataset you will be working with
  • Set yourself up with a dedicated directory that has the data in it
  • Make sure to bring to the exam:
    • something to write with (black/blue pen or pencils)
    • your laptop (& a charging chord)

Midterm - Coding Portion

  • You may use course and online resources other but not generative AI
  • All questions can be addressed with just the course materials
    • this includes cheatsheets, course slides, or course assignments (anything linked through the course materials page or Canvas)
  • You must cite any resources that are not the course materials
    • include the prompt for Google searchers
  • I expect what you submit is your work

Caution

While the coding tasks allow resources, you will likely run out of time if you have to look everything up. Know what functions you might need and where to find documentation for implementing these functions.

Midterm Preparation Suggestions

  • Review course slides, handouts, & Quizzes
  • Quiz each other on the uses of different functions
  • Try to re-do parts of the PAs or LAs from scratch
  • Start working with the data
    • HAVE CODE SET UP THAT READS IN THE DATA
    • Ask some questions about the data and try to answer them
  • Save example code for things you find tricky in a place you can find it
  • Get sleep and feed yourself! 🛌🥞🥙🍛

Midterm - Practice Gradescope Submission

  • Rather than an .html to directly to Canvas, you will submit a rendered .pdf to Gradescope through Canvas
  • Let’s make sure this works now!

Date + Time Variables

lubridate

  • Convert a date-like variable (“May 8, 1995”) to a date or date-time object.

  • Find the weekday, month, year, etc from a date-time object.

  • Convert between time zones.

The image shows the hex logo for the lubridate R package. The logo is a green hexagon with a stylized calendar in the center. The calendar has a small clock icon overlapping its bottom left corner, symbolizing time-related functions. The text 'lubridate' appears prominently below the calendar icon within the hexagon. Lubridate is commonly used in R for working with date and time data.

Note

The lubridate package installs and loads with the tidyverse.

Why are dates and times tricky?

When parsing dates and times, we have to consider complicating factors like…

  • Daylight Savings Time.
    • One day a year is 23 hours; one day a year is 25 hours.
    • Some places use it, some don’t.
  • Leap years – most years have 365 days, some have 366.
  • Time zones.

Creating date-time Objects

Big Picture

There are a lot of diferent ways to create date-time objects!

Create a date from individual components:

make_date(year = 1995, month = 05, day = 08)
[1] "1995-05-08"

Create a date from a number:

dmy(08051995)
[1] "1995-05-08"
ymd(19950508)
[1] "1995-05-08"

Create a date-time Object from a String

mdy("August 29, 1991")
[1] "1991-08-29"
dmy("29-August-1991", 
    tz = "America/Denver")
[1] "1991-08-29 MDT"
dmy_hms("29-August-1991 9:32:12", 
        tz = "America/Denver")
[1] "1991-08-29 09:32:12 MDT"
as_datetime("91-08-29", 
            format = "%y-%m-%d")
[1] "1991-08-29 UTC"
parse_datetime("8/29/1991", 
               format = "%m/%d/%Y")
[1] "1991-08-29 UTC"

Creating date-time Objects

Extracting date-time Components

bday <- ymd_hms("1995-02-27 07:03:12", 
                tz = "America/Chicago")
bday
[1] "1995-02-27 07:03:12 CST"


year(bday)
[1] 1995
month(bday)
[1] 2
day(bday)
[1] 27
wday(bday)
[1] 2
wday(bday, 
     label = TRUE, 
     abbr = FALSE)
[1] Monday
7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

Subtraction with date-time Objects

Doing subtraction gives you a difftime object.

difftime objects do not always have the same units – it depends on the scale of the objects you are working with.

How old am I?

today() - mdy("02-27-1995")
Time difference of 11397 days


How long did it take me to type this slide?

begin <- mdy_hms("10/21/2024 20:40:34")
finish <- mdy_hms("10/21/2024 20:43:11")

finish - begin
Time difference of 2.616667 mins

Durations and Periods

Durations will always give the time span in an exact number of seconds.

as.duration(
  today() - mdy("02-27-1995")
            )
[1] "984700800s (~31.2 years)"

Periods will give the time span in more approximate, but human readable times.

as.period(
  today() - mdy("02-27-1995")
  )
[1] "11397d 0H 0M 0S"

Durations and Periods

We can also add time to date-time objects:

  • ddays(), dyears(), etc. will add a duration of time.
  • days(), years(), etc. will add a period of time.

Because durations use the exact number of seconds to represent days and years, you might get unexpected results.


When is is my 99th birthday?

mdy("02/27/1995") + dyears(99)
[1] "2094-02-26 18:00:00 UTC"
mdy("02/27/1995") + years(99)
[1] "2094-02-27"

Intervals

Sppose Delta requires members to travel within 4 weeks before or after their birthday to earn a “Birthday Bonus Flight.”

Well, we first need to make an interval of time around my birthday.

bday <- mdy("02-27-2026")

bonus_flight_window <- interval(
  bday - weeks(4),  
  bday + weeks(4)
)

Then I can check if today is within this interval.

today() %within% bonus_flight_window
[1] FALSE

Time Zones…

…are complicated!


Time zones have specific values in lubridate. Most are specified as:

  • {continent}/{city} – “America/Denver”, “Africa/Nairobi”
  • {ocean}/{city} – “Pacific/Auckland” . . .

The US time-zones can be specified as:

  • “US/Eastern”, “US/Central”, “US/Mountain”, and “US/Pacific”

What time zone does R think I’m in?

Sys.timezone()
[1] "America/Los_Angeles"

Time Zones

You can change the time zone of a date in two ways:

x <- ymd_hms("2024-10-24 18:00:00", 
             tz = "Europe/Copenhagen")

with_tz()

Keeps the instant in time the same, but changes the visual representation.

x |> 
  with_tz()
[1] "2024-10-24 09:00:00 PDT"
x |> 
  with_tz(tzone = "Asia/Kolkata")
[1] "2024-10-24 21:30:00 IST"

force_tz()

Changes the instant in time by forcing a time zone change.

x |> 
  force_tz()
[1] "2024-10-24 18:00:00 PDT"
x |> 
  force_tz(tzone = "Asia/Kolkata")
[1] "2024-10-24 18:00:00 IST"

Common Mistake with Dates

When you read data in or create a new date-time object, the default time zone (if not specified) is UTC (Universal Time Coordinated)*.

So, make sure you specify your desired time zone!

x <- mdy("11/20/1993")
tz(x)
[1] "UTC"
x <- mdy("11/20/1993", 
         tz = "America/Los_Angeles")
tz(x)
[1] "America/Los_Angeles"

*UTC is the same as GMT (Greenwich Mean Time) which is where Iceland is located

Working with Dates in Data

  • the lubridate functions take a vector as the first argument
  • when working with data, we need to combine lubridate functions with dplyr verbs!
dat
# A tibble: 3 × 2
  date       event                  
  <chr>      <chr>                  
1 05-04-2026 may the 4th be with you
2 05-01-2026 may day                
3 06-01-2026 pride month            
dat |>
  mutate(date = mdy(date),
         month = month(date)
         )
# A tibble: 3 × 3
  date       event                   month
  <date>     <chr>                   <dbl>
1 2026-05-04 may the 4th be with you     5
2 2026-05-01 may day                     5
3 2026-06-01 pride month                 6

Tips for Working with Dates

  • Always just check that you are getting results that you expect!
  • Pay attention to time zones
  • Use the lubridate cheatsheet

PA 5.2: Jewel Heist

  • Use dates from clues to find the jewel thief!
  • Make sure to pay attention to time zones ⏰

Movie poster for The Pink Panther, a movie and TV show series. Shows a detective following paw prints with a magnifying glass, but a shadow of the pink panther looming over them looking mischieveous.

LA 5: Murder in SQL City

  • This lab looks different!
  • You will need a number of steps to follow the clues - it won’t be done in one pipeline
  • Read the instructions carefully
  • At the end, try to delete any code or output that you don’t actually need
  • Check with others if you are stuck! You can see if they get the witness or clues answers at that step.

T

To do…

  • PA 5.2: Jewel Heist
    • due Friday (5/1) at 11:59pm
  • Lab 5: Murder in SQL City
    • due Sunday (5/3) at 11:59pm
  • Required Reading
  • Review all material from this week for the group quiz on Monday!
  • ⚠️ Class Setup: install git and setup GitHub
    • due Sunday (5/3) at 11:59pm
  • Project Checkpoint 1: Group Formation Survey
    • due Monday (5/4) at 11:59pm