This is the second part in the series teaching the “lubridate” package.
As a short recap from the previous part, I mentioned that date/date_time formats are easily parced using the
ymd set of functions (for example,
I also explained that arithmetic calculations are performed using the days, months, years, etc. functions.
In this part, I’ll cover the following topics:
1. Explanation of “instant” and “interval” concepts
2. Setting/Extracting date/time parts
3. Working with Time Zones.
In case you skipped the first part, let’s start by downloading and installing lubridate package:
Answers to the exercises are available here.
If you have different solutions, feel free to post them.
“lubridate” has several options for representing dates and times, some of them are “absolute” time spans and others are “relative” ones (for example, think about adding “one month” to a specific date, do you need take into account issues such as leap year or daylight saving times.
The possible date/time representations in “lubridate” are instant , interval, duration (exact span of time) and period (relative span of time).
The “instant” concept
As stated in the paper that introduced “lubridate” package (please refer to “Journal of Statistical Software”, April 2011, Volume 40, Issue 3), “An instant is a specific moment in time, such as January 1st, 2012. We create an instant each time we parse a date into R.”
We can check if an object is an instant by using
Based on a “start” instant and an “end” instant, we can create an exact interval.
The length of an interval is never ambiguous, because we know when it occurs.
We can create interval objects by using the command
Although “durations” and “periods” are more common when doing date/time calculations, it is important to know also the “instant” and “interval” concepts.
Populate two variables called
"end_date" with date representation of string “01/12/2015 15:40:32” and “01/10/2016 16:01:10”
Create an interval variable called my_interval based on variables “start_date” and “end_date”
Show the class of the new object in order to make sure it’s an “interval”
Setting/Extracting date/time parts:
You can extract and set any part of a date/time variable using the following functions:
wday(x, label = FALSE, abbr = TRUE)
Feel free playing with the different options in the “wday” function.
Of course, instead of extracting, you can set a specific value for each part of a date/time variable.
Extract the “week day” part from the “start_date” variable created on exercise 1
Display the name of the day in a long format.
Check if the “day of the year” in the “end_date” variable is greater than 230
Time zones represent the same “instant” across different geographic locations.
For example “2016-08-21 11:53:24” in UCT time zone is identical “2016-08-21 19:53:24” in CST TZ.
“lubridate” provides two functions that help dealing with time zones:
with_tz() : This function only changes the representation of the specific instant, but not its actual value
force_tz() : This function changes the actual value of a specific instant.
Tip 1: use
OlsonNames() to get a list of time zones defined in your machine
Tip 2: use
grep("country_name",OlsonNames(),value=TRUE) to find a specific time zone
Find the time zone representation for the city of “Buenos_Aires”
Display the value of “end_date” variable according to “Buenos Aires” time zone
Populate a variable called “NY_TZ” with the time zone value of “New_York” city
Set the time zone of “end_date” so it matches the time zone of New York. Populate a variable called “end_date_ny” with the result
Display the time difference between end_date_ny and end_date
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below