How to work with dates in R?

Introduction


In this module, we will learn to work with date/time data in R using lubridate, an R package that makes it easy to work with dates and time. Let us begin by installing and loading the lubridate pacakge.

Origin


lubridate::origin()

Current Date/Time


now()
today()
am(now())  
pm(now())

Data


transact <- read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/transact.csv')
transact

Data Dictionary


The data set has 3 columns. All the dates are in the format (yyyy-mm-dd).

  • Invoice: invoice date
  • Due: due date
  • Payment: payment date

Case Study


We will use the functions in the lubridate package to answer a few questions we have about the transact data.

  • extract date, month and year from Due
  • compute the number of days to settle invoice
  • compute days over due
  • check if due year is a leap year
  • check when due day in february is 29, whether it is a leap year
  • how many invoices were settled within due date
  • how many invoices are due in each quarter
  • what is the average duration between invoice date and payment date

Extract Date, Month & Year


Extract Date, Month & Year


this_day <- as_date('2017-03-23')
day(this_day)
month(this_day)
year(this_day)

Extract Date, Month & Year


transact %>%
  mutate(
    due_day   = day(Due),
    due_month = month(Due),
    due_year  = year(Due)
  )

Course Duration


Course Duration


course_start    <- as_date('2017-04-12')
course_end      <- as_date('2017-04-21')
course_duration <- course_end - course_start
course_duration

Date Arithmetic


Compute days to settle invoice

transact %>%
  mutate(
    days_to_pay = Payment - Invoice
  )

Date Arithmetic


Compute days over due

transact %>%
  mutate(
    delay = Payment - Due
  )

Leap Year

Is due year a leap year?


transact %>%
  mutate(
    due_year = year(Due),
    is_leap  = leap_year(due_year)
  )

Leap Year

If due day is February 29, is it a leap year?


transact %>%
  mutate(
    due_day   = day(Due),
    due_month = month(Due),
    due_year  = year(Due),
    is_leap   = leap_year(due_year)
  ) %>%
  select(-(Invoice), -(Payment)) %>%
  filter(due_month == 2 & due_day == 29)

Shift Date


Shift Date


course_start + days(2)
course_start + weeks(1)
course_start + years(1)

Interval


Interval


interval(course_start, course_end)

Shift Interval


Shift Interval


course_interval <- interval(course_start, course_end)
course_interval %>%
  int_shift(by = days(1))
course_interval %>%
  int_shift(by = weeks(1))
course_interval %>%
  int_shift(by = years(1))

Intervals Overlap


Intervals Overlap


vacation_start    <- as_date('2017-04-19')
vacation_end      <- as_date('2017-04-25')
vacation_interval <- interval(vacation_start, vacation_end)
int_overlaps(course_interval, vacation_interval)

Case Study


How many invoices were settled within due date?

transact %>%
  mutate(
    inv_due_interval = interval(Invoice, Due),
    due_next         = Due + days(1),
    due_pay_interval = interval(due_next, Payment),
    overlaps         = int_overlaps(inv_due_interval, due_pay_interval)
  ) %>%
  select(Invoice, Due, Payment, overlaps)

Case Study


How many invoices were settled within due date?

# using int_shift
transact %>%
  mutate(
    inv_due_interval = interval(Invoice, Due),
    due_pay_interval = interval(Due, Payment),  
    due_pay_next     = int_shift(due_pay_interval, by = days(1)),
    overlaps         = int_overlaps(inv_due_interval, due_pay_next)
  ) %>%
  select(Invoice, Due, Payment, overlaps)

Within


Within


conference <- as_date('2017-04-15')
conference %within% interval(course_start, course_end)

Case Study


How many invoices were settled within due date?

transact %>%
  mutate(
    inv_due_interval = interval(Invoice, Due),
    overlaps = Payment %within% inv_due_interval
  ) %>%
  select(Due, Payment, overlaps)

Quarter


Quarter


course_start
course_start %>%
  quarter()
course_start %>%
  quarter(with_year = TRUE)

Quarter


transact %>%
  mutate(
    Quarter = quarter(Due)
  )

Case Study


transact %>%
  mutate(
    Quarter = quarter(Due, with_year = TRUE)
  )

Duration


course_duration %>%
  as.numeric() %>%
  duration(units = 'days')

Convert


Case Study


interval(course_start, course_end) / dseconds()
interval(course_start, course_end) / dminutes()
interval(course_start, course_end) / dhours()
interval(course_start, course_end) / dweeks()
interval(course_start, course_end) / dyears()

Convert


Case Study


interval(course_start, course_end) %>%
  time_length(unit = "seconds")
interval(course_start, course_end) %>%
  time_length(unit = "minutes")
interval(course_start, course_end) %>%
  time_length(unit = "hours")

Convert


Case Study


interval(course_start, course_end) %>%
  as.period(unit = "seconds")
interval(course_start, course_end) %>%
  as.period(unit = "minutes")
interval(course_start, course_end) %>%
  as.period(unit = "hours")