Dates and Times – Simple and Easy with lubridate exercises (part 2)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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, dmy
, ymd_h
, etc).
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:
install.packages("lubridate")
library(lubridate)
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 is.instant()
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 interval()
.
Although “durations” and “periods” are more common when doing date/time calculations, it is important to know also the “instant” and “interval” concepts.
Exercise 1
Populate two variables called "start_date"
and "end_date"
with date representation of string “01/12/2015 15:40:32” and “01/01/2016 16:01:10”
Exercise 2
Create an interval variable called my_interval based on variables “start_date” and “end_date”
Exercise 3
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:
day(x)
mday(x)
wday(x, label = FALSE, abbr = TRUE)
qday(x)
yday(x)
hour(x)
minute(x)
second(x)
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.
Exercise 4
Extract the “week day” part from the “start_date” variable created on exercise 1
Display the name of the day in a long format.
Exercise 5
Check if the “day of the year” in the “end_date” variable is greater than 230
Time Zones
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
Exercise 6
Find the time zone representation for the city of “Buenos_Aires”
Exercise 7
Display the value of “end_date” variable according to “Buenos Aires” time zone
Exercise 8
Populate a variable called “NY_TZ” with the time zone value of “New_York” city
Exercise 9
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
Exercise 10
Display the time difference between end_date_ny and end_date
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.