<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/"></div></div><div><a data-url="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/" data-counturl="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/" data-count="horizontal" data-text="Dates and Times – Simple and Easy with lubridate exercises (part 2)" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/08/29/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://r-exercises.com/wp-content/uploads/2016/08/Philadelphia-Citys-Hall-Clock.png?81d732"><img sizes="(max-width: 150px) 100vw, 150px" alt="Philadelphia - City's Hall Clock" class="wp-image-1246" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2016/08/Philadelphia-Citys-Hall-Clock-300x260.png?resize=150%2C150" data-recalc-dims="1"></a>This is the second part in the <a target="_blank" href="http://r-exercises.com/tag/lubridate/">series</a> teaching the “lubridate” package.</p>
<p>As a short recap from the previous part, I mentioned that date/date_time formats are easily parced using the <code>ymd</code> set of functions (for example, <code>dmy</code>, <code>ymd_h</code>, etc).<br>
I also explained that arithmetic calculations are performed using the days, months, years, etc. functions.</p>
<p>In this part, I’ll cover the following topics:<br>
1. Explanation of “instant” and “interval” concepts<br>
2. Setting/Extracting date/time parts<br>
3. Working with Time Zones.</p>
<p>In case you skipped the first part, let’s start by downloading and installing lubridate package:</p>
<p><code> install.packages(&quot;lubridate&quot;) </code><br>
<code> library(lubridate) </code></p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/08/29/dates-and-times-simple-and-easy-with-lubridate-solutions-part-2/">here</a>.<br>
If you have different solutions, feel free to post them.</p>
<p> “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.<br>
The possible date/time representations in “lubridate” are <strong>instant</strong> , <strong>interval</strong>, <strong>duration</strong> (exact span of time) and <strong>period</strong> (relative span of time).</p>
<h4> The “instant” concept </h4>
<p>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.”<br>
We can check if an object is an instant by using <code> is.instant() </code></p>
<p>Based on a “start” instant and an “end” instant, we can create an exact <strong>interval</strong>.<br>
The length of an interval is never ambiguous, because we know when it occurs.<br>
We can create interval objects by using the command <code>interval() </code>.<br>
Although “durations” and “periods” are more common when doing date/time calculations, it is important to know also the “instant” and “interval” concepts.</p>
<p><strong>Exercise 1</strong><br>
Populate two variables called <code>&quot;start_date&quot;</code> and <code>&quot;end_date&quot;</code> with date representation of string “01/12/2015 15:40:32” and “01/01/2016 16:01:10”</p>
<p><strong>Exercise 2</strong><br>
Create an interval variable called my_interval based on variables “start_date” and “end_date”</p>
<p><strong>Exercise 3</strong><br>
Show the class of the new object in order to make sure it’s an “interval”</p>
<h4>Setting/Extracting date/time parts:</h4>
<p>You can extract and set any part of a date/time variable using the following functions:<br>
<code> day(x) </code><br>
<code> mday(x) </code><br>
<code> wday(x, label = FALSE, abbr = TRUE) </code><br>
<code> qday(x) </code><br>
<code> yday(x) </code><br>
<code> hour(x) </code><br>
<code> minute(x) </code><br>
<code> second(x) </code></p>
<p>Feel free playing with the different options in the “wday” function.</p>
<p>Of course, instead of extracting, you can set a specific value for each part of a date/time variable.</p>
<p><strong>Exercise 4</strong><br>
Extract the “week day” part from the “start_date” variable created on exercise 1<br>
Display the name of the day in a long format.</p>
<p><strong>Exercise 5</strong><br>
Check if the “day of the year” in the “end_date” variable is greater than 230</p>
<h4>Time Zones</h4>
<p>Time zones represent the same “instant” across different geographic locations.<br>
For example “2016-08-21 11:53:24” in UCT time zone is identical “2016-08-21 19:53:24” in CST TZ.<br>
“lubridate” provides two functions that help dealing with time zones:<br>
<code> with_tz() </code> : This function only changes the representation of the specific instant, but not its actual value <code> force_tz() </code>: This function changes the actual value of a specific instant.</p>
<p> Tip 1: use <code> OlsonNames() </code> to get a list of time zones defined in your machine<br>
Tip 2: use <code> grep(&quot;country_name&quot;,OlsonNames(),value=TRUE) </code> to find a specific time zone</p>
<p><strong>Exercise 6</strong><br>
Find the time zone representation for the city of “Buenos_Aires”</p>
<p><strong>Exercise 7</strong><br>
Display the value of “end_date” variable according to “Buenos Aires” time zone</p>
<p><strong>Exercise 8</strong><br>
Populate a variable called “NY_TZ” with the time zone value of “New_York” city</p>
<p><strong>Exercise 9 </strong><br>
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</p>
<p><strong>Exercise 10 </strong><br>
Display the time difference between end_date_ny and end_date</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/"></div></div><div><a data-url="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/" data-counturl="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/" data-count="vertical" data-text="Dates and Times – Simple and Easy with lubridate exercises (part 2)" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/08/29/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/EqlV6wo8l3o"></a></div>