lubridate
to Work with DatesToday we will…
Caution
While the coding tasks are open-resource, 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.
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.
Note
The lubridate
package installs and loads with the tidyverse
.
When parsing dates and times, we have to consider complicating factors like…
date-time
ObjectsThere are multiple data types for dates and times.
date
or Date
dtm
POSIXlt
– stores date-times as the number of seconds since January 1, 1970 (“Unix Epoch”)POSIXct
– stores date-times as a list with elements for second, minute, hour, day, month, year, etc.date-time
ObjectsBig Picture
There are a lot of diferent ways to create date-time
objects!
date-time
Object from a Stringdate-time
ObjectsWhat’s wrong here?
Make sure you use quotes!
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:
days()
, years()
, etc. will add a period of time.ddays()
, dyears()
, etc. will add a duration of time.…are complicated!
Specify time zones in the form:
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)*.