Package Overview: Lubridate

  • The main purpose of Lubridate is to make working with dates and times simpler.
  • Methods for dealing with date-time objects must be robust to changes in time such as leap years, time zones, daylight-savings time, seasons, and many other unique aspects of time.
  • With this package one can manipulate, parse, and extract date/time data as well as perform algebraic operations
    • Grouping these tools by common purpose is helpful
  • Uses a very memorable syntax that makes working with Lubridate very easy
  • Examples of function groups
    • Parsing Dates
    • Manipulating Dates
    • Rounding Dates
    • Time Zones
    • Durations
    • Intervals
  • Main authors of Lubridate are Garrett Grolemund and Hadley Wickham.
  • Lubridate currently operates on version 1.7.9.

Necessary Packages

library(tidyverse)
library(knitr)
library(lubridate)

Examples of Usage

as_date

as_date(): this function converts an object into a date or date-time

vect <- c("04/26/2008", "12/25/1990", "01/01/2000")
class(vect)
## [1] "character"
# %Y-%m-%d is the default format, the following line fails 
vect_dt <- as_date(vect)
## Warning: All formats failed to parse. No formats found.
vect_dt <- as_date(vect, format = "%m/%d/%Y")
vect_dt
## [1] "2008-04-26" "1990-12-25" "2000-01-01"
class(vect_dt)
## [1] "Date"

Get/Set Functions

x <- as_date("2005-05-10 5:17:42")
day(x)
## [1] 10
day(x) <- 17
day(x)
## [1] 17
x
## [1] "2005-05-17"
minute(x)
## [1] 0
y <- ("2005-05-10 5:17:42")
hour(y)
## [1] 5
second(y)
## [1] 42
minute(y)
## [1] 17
month(y)
## [1] 5
year(y)
## [1] 2005

Date “Math”

y <- as_date("2018-10-26")
z <- y + days(2)
z
## [1] "2018-10-28"
reg_time <- now(tzone = 'EST')
daylight_svngs <- reg_time + hours(1)
reg_time
## [1] "2020-10-10 11:44:23 EST"
daylight_svngs
## [1] "2020-10-10 12:44:23 EST"
#adding durations and intervals together
start_2020 <- ymd_hms("2020-01-01 12:00:00")
new_week <- ddays(7)
first_week_2020 <- start_2020 + new_week
first_week_2020
## [1] "2020-01-08 12:00:00 UTC"
tm_corona <- ymd_hm("2020-03-12 11:45", tz = "US/Eastern")
tm_now <- now()
difftime(tm_now, tm_corona, units = "weeks")
## Time difference of 30.29161 weeks
christmas <- ymd("2020-12-25")
new_year <- ymd("2021-01-01")
interval <- new_year - christmas

z <- interval/ddays(1)
z
## [1] 7

Graph Using Time Data

Code

str(lakers$date)
lakers$date <- ymd(lakers$date)
str(lakers$date)
lakers$game_type <- as.factor(lakers$game_type)
home_away <<- ggplot(lakers, aes(x = date, y = 0)) + 
  geom_point(aes(color = game_type)) + 
  labs(title = "2008 Home and Away Games for the Lakers Over Time") +
  xlab("Date of Game") +
  ylab("") +
  scale_y_continuous(breaks = seq(0, 1, 1)) + 
  scale_color_discrete("Home or Away Game")
  theme(axis.text.y = element_blank()
)

Graph

home_away

Our Opinion

Pros:

  • Lubridate is the perfect package for working with date-time data. Nearly all the benefits of lubridate involve making date-time data manipulation much easier. Here are the key benefits that lubridate offers as a package in R.
  • Much better than other R functions for manipulating and performing operations on time data.
    • Easier to identify and parse date-time data
    • Easier to extract specific components of a date-time, like year, month, days, hours, minutes
    • Easier to perform accurate calculations with date-time
    • Easier to handle time zones and daylight savings time
  • Time data is made it’s own date type, depending on the method used
  • Uses an intuitive user interface
  • Syntax is much more easy to use and more memorable
  • Includes functions that apply to every combination of how dates can be represented
  • Useful for comparing time data to each other

Link to Download Graphic

Cons:

  • Beyond compatibility with other packages in R, lubridate does not have many cons in terms of functionality. Created as a solution to dealing with date-time data, lubridate serves to do just that.
  • For its default setting Lubridate uses time instants expressed in UTC, which isn’t associated with a time zone and does not take into account daylight savings
  • There isn’t complete compatibility between lubridate and dplyr, particularly in older versions of the softwares
    • Ex: dplyr can’t deal with the duration class, would need to convert to numeric