<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-1/"></div></div><div><a data-url="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-1/" data-counturl="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-1/" data-count="horizontal" data-text="Dates and Times – Simple and Easy with lubridate exercises (part 1)" 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/15/dates-and-times-simple-and-easy-with-lubridate-part-1/"> 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/Big-Ben-clock-tower.png?8ac47c"><img sizes="(max-width: 300px) 100vw, 300px" alt="Big Ben clock tower" class="wp-image-1145" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2016/08/Big-Ben-clock-tower-300x171.png?resize=300%2C171" data-recalc-dims="1"></a><br>
As in any programming language, handling date and time variables can be quite frustrating, since, for example, there is no one single format for dates, there are different time zones and there are issues such as daylight saving time.</p>
<p>Base R provides several packages for handling date and time variables, but they require mastering cumbersome syntax.</p>
<p>In order to solve all those issues and more, R package “lubridate” was created. This package on one hand has a very easy and intuitive syntax and on the other hand has functions that cover a wide range of issues related to dates and times.</p>
<p>In this first part in the series of lubridate exercises, the basic functionality of the package is covered.</p>
<p>As always, let’s start by downloading and installing the 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/15/dates-and-times-simple-and-easy-with-lubridate-part-1-2/">here</a>.<br>
If you have different solutions, feel free to post them.</p>
<p><strong>
<h2>Parsing Date-times</h2>
<p><br>
The <strong>ymd()</strong> series of functions are used to parse character strings into dates.<br>
The letters y, m, and d correspond to the year, month, and day elements of a date-time.</p></strong>
<p><strong>Exercise 1</strong><br>
Populate a variable called “start_date” with a date representation of string “23012017”</p>
<p><strong>Exercise 2</strong><br>
Use the lubridate function <code>today</code> to print the current date</p>
<p><strong>Exercise 3</strong><br>
Extract the <code>year</code> part from the “start_date” variable created on exercise 1</p>
<p><strong>Exercise 4</strong><br>
Extract the <code>month</code> part from the “start_date” variable created on exercise 1</p>
<p><strong>Exercise 5</strong><br>
Extract the <code>day</code> part from the “start_date” variable created on exercise 1</p>
<p><strong>Exercise 6</strong><br>
Set the <code>month</code> in variable “start_date” to February</p>
<p><strong>Exercise 7</strong><br>
Add 6 <code>days</code> to variable “start_date”.<br>
Did you notice what happened to the month value?</p>
<p><strong>Exercise 8</strong><br>
Substract 3 <code>months</code> from variable “start_date”</p>
<p><strong>Exercise 9 (Advanced) </strong><br>
Populate a field called concatenated_dates with a vector of dates containing the following values:<br>
“31.12.2015”, “01.01.2016”, “15.02.2016”</p>
<p><strong>Exercise 10 (Advanced) </strong><br>
Calculate in a short and simple way the addition of 1 thru 10 days to “start_date” variable</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-1/"></div></div><div><a data-url="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-1/" data-counturl="https://www.r-bloggers.com/dates-and-times-simple-and-easy-with-lubridate-exercises-part-1/" data-count="vertical" data-text="Dates and Times – Simple and Easy with lubridate exercises (part 1)" 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/15/dates-and-times-simple-and-easy-with-lubridate-part-1/"> 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/iAXbz1ejc90"></a></div>