<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-3/"></div></div><div><a data-url="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-3/" data-counturl="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-3/" data-count="horizontal" data-text="Dates and Times – Simple and Easy with lubridate Exercises (part 3)" 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/10/04/dates-and-times-simple-and-easy-with-lubridate-exercises-part-3/"> 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="https://i2.wp.com/r-exercises.com/wp-content/uploads/2016/10/The-Prague-Astronomical-Clock.jpg?resize=259%2C194"><img alt="the-prague-astronomical-clock" class="wp-image-2127" src="https://i2.wp.com/r-exercises.com/wp-content/uploads/2016/10/The-Prague-Astronomical-Clock.jpg?resize=259%2C194" data-recalc-dims="1"></a><br>
Welcome to the third and last part of the “lubridate” exercises. If you missed Part 1 and 2 then please refer to the links below:</p>
<p><a target="_blank" href="http://r-exercises.com/2016/08/15/dates-and-times-simple-and-easy-with-lubridate-part-1/">Part 1</a><br>
<a target="_blank" href="http://r-exercises.com/2016/08/29/dates-and-times-simple-and-easy-with-lubridate-exercises-part-2/">Part 2</a></p>
<p>In this part, I’ll cover the following topics:<br>
1. Durations (exact spans of time)<br>
2. Periods (relative spans of time)<br>
3. Rounding dates </p>
<p>As always, in case you skipped the previous parts, 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/10/04/dates-and-times-simple-and-easy-with-lubridate-solutions-part-3/">here</a>.<br>
If you have different solutions, feel free to post them.</p>
<h2> Durations and Periods </h2>
<p>“lubridate” package has four types of “date/time” objects (Can be remembered by the acronym “IIDP”) :<br>
1. <b>Instant</b>: A specific moment in time (this concept is explained in the previous part).<br>
2. <b>Interval</b>: Based on “start” instant and “end” instant we create an “interval” (this concept is also explained in the previous part).<br>
3. <b>Duration</b>: Following the concept of “interval”, we can “remove” the <i>start</i> and <i>end</i> points and what will remain is an <b>exact time period</b>, which can be measured in seconds.<br>
Thus, <b>durations</b> are fixed time periods, not influenced by leap years or other time changes.<br>
Durations are recommended when comparing time-based objects, such as speeds, execution times, etc.<br>
A duration object is created, for example, using the <code> duration() </code> function.<br>
Functions related to durations start with a <b> d </b> (for example, <code>dseconds</code>, <code>dminutes</code> and <code>dyears</code>).<br>
4. <b> Period</b>: Time spans measured in units larger than seconds (and thus taking into consideration leap years, daylight saving time and similar “time-related” issues), are called periods.<br>
Thus, <b>periods</b> are relative time periods, influenced by leap years or other time changes.<br>
Periods are used usually when calculating differences in calendar dates.<br>
Functions related to durations start <b>without a d </b> (for example, <code>days</code>, <code>minutes</code> and <code>years</code>).</p>
<h2> Rounding dates </h2>
<p>Like numbers, date-times occur in order. This allows date-times to be rounded. lubridate<br>
provides three methods that help perform this rounding: <code>round_date()</code>, <code>floor_date()</code>, and <code>ceiling_date()</code>.<br>
The first argument of each function is the date-times to be rounded. The second argument is the unit to round to.</p>
<p><strong>Exercise 1</strong><br>
Create a <b> duration </b> object having 260 seconds</p>
<p><strong>Exercise 2</strong><br>
Create a <b> duration </b> object having 260 minutes and divide it by 60</p>
<p><strong>Exercise 3</strong><br>
Create a <b> duration </b> object of 1 day length and substract from it 260 duration minutes</p>
<p><strong>Exercise 4</strong><br>
Create a <b> duration </b> object of 12 days, 5 hours and 10 minutes</p>
<p><strong>Exercise 5</strong><br>
Create a sequence of periods, from zero to 10, by 2 weeks</p>
<p><strong>Exercise 6</strong><br>
round date ‘2016-10-16’ by ‘month’ unit</p>
<p><strong>Exercise 7</strong><br>
round date ‘2016-10-16’ down, by ‘month’ unit</p>
<p><strong>Exercise 8</strong><br>
round date ‘2016-10-16’ up, by ‘month’ unit</p>
<p><strong>Exercise 9 and 10 </strong><br>
Calculating <i> Thanksgiving </i> Day in 2016:<br>
Thanksgiving is celebrated on the fourth Thursday of November.<br>
So we will execute the following steps:<br>
1. Create a date/time object (date1 variable) representing Novevember 1st, 2016<br>
2. Check which day of the week November 1st is.<br>
3. Add the needed days to reach the upcoming Thursday (date2 variable)<br>
4. Make sure your calculation is correct (e.g. you reached the upcoming Thursday)<br>
5. Add 3 weeks </p>
<p>Congratulations and happy Thanksgiving !!</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-3/"></div></div><div><a data-url="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-3/" data-counturl="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-3/" data-count="vertical" data-text="Dates and Times – Simple and Easy with lubridate Exercises (part 3)" 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/10/04/dates-and-times-simple-and-easy-with-lubridate-exercises-part-3/"> 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/9_YpkgtNguU"></a></div>