lubridate to Work with DatesToday we will…
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.
lubridateConvert 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.
Note
The lubridate package installs and loads with the tidyverse.
When parsing dates and times, we have to consider complicating factors like…
date-time ObjectsBig Picture
There are a lot of diferent ways to create date-time objects!
Create a date from individual components:
date-time Object from a Stringdate-time Objects
date-time Componentsdate-time ObjectsDoing 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.
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.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.
Then I can check if today is within this interval.
…are complicated!
Time zones have specific values in lubridate. Most are specified as:
The US time-zones can be specified as:
You can change the time zone of a date in two ways:
with_tz()
When you read data in or create a new date-time object, the default time zone (if not specified) is UTC (Universal Time Coordinated)*.
lubridate functions take a vector as the first argumentlubridate functions with dplyr verbs!