<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/span-dates-and-times-without-overhead/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://edwinth.github.io/blog/span_date_time/"> That’s so Random</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am working on v.0.4.0 of the <a rel="nofollow" href="https://cran.r-project.org/web/packages/padr/vignettes/padr.html" target="_blank">padr package</a> this summer. Two new features that will be added are wrappers around <code>seq.Date</code> and <code>seq.POSIXt</code>. Since it is going to take a while before the new release is on CRAN, I go ahead and do an early presentation of these functions. Date and datetime parsing in base R are powerful and comprehensive, but also tedious. They can slow you down in your programming or analysis. Luckily, good wrappers and alternatives exist, at least the <code>ymd{_h}{m}{s}</code> suite from <a rel="nofollow" href="https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html" target="_blank">lubridate</a> and Dirk Eddelbuettel’s <a rel="nofollow" href="https://cran.r-project.org/web/packages/anytime/README.html" target="_blank">anytime</a>. These functions remove much of the overhead of date and datetime parsing, allowing for quick formatting of vectors in all kinds of formats. They also alleviate the pain of using <code>seq.Date()</code> and <code>seq.POSIXt</code> a little, because the <code>from</code> and the <code>to</code> arguments should be parsed dates or datetimes. Take the following example.</p>
<div>
<pre><code><span>seq</span><span>(</span><span>as.POSIXct</span><span>(</span><span>&quot;2017-07-25 00:00:00&quot;</span><span>),</span><span> </span><span>as.POSIXct</span><span>(</span><span>&quot;2017-07-25 03:00:00&quot;</span><span>),</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;hour&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2017-07-25 00:00:00 CEST&quot; &quot;2017-07-25 01:00:00 CEST&quot;
## [3] &quot;2017-07-25 02:00:00 CEST&quot; &quot;2017-07-25 03:00:00 CEST&quot;
</code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>lubridate</span><span>)</span><span>
</span><span>seq</span><span>(</span><span>ymd_h</span><span>(</span><span>&quot;20170725 00&quot;</span><span>),</span><span> </span><span>ymd_h</span><span>(</span><span>&quot;20170725 03&quot;</span><span>),</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;hour&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2017-07-25 00:00:00 UTC&quot; &quot;2017-07-25 01:00:00 UTC&quot;
## [3] &quot;2017-07-25 02:00:00 UTC&quot; &quot;2017-07-25 03:00:00 UTC&quot;
</code></pre>
</div>
<p>I think, however, that there is still some overhead in the second specification. By overhead I mean specifying things that feel redundant, things that could be set to some kind of default. Since the whole idea behind <code>padr</code> is automating away redundant and tedious actions in preparing datetime data, providing alternative functions that ask for as little as possible are a natural addition. This resulted in <code>span_date</code> and <code>span_time</code>. They remove overhead by:</p>
<ul>
<li>
<p>allowing for specification of <code>from</code> and <code>to</code> directly as integer or character in <em>lubridatish</em> format.</p>
</li>
<li>
<p>setting the unspecified datetime parts to a default of 1 for month and day, and 0 for hour, minute, and second.</p>
</li>
<li>
<p>assuming the desired interval (the <code>by</code> statement in <code>seq.Date</code> and <code>seq.POSIXt</code>) as the lowest of the datetime parts specified in either <code>from</code> or <code>two</code>.</p>
</li>
</ul>
<p>If this is a little abstract still, let me give some examples. The above becomes example becomes:</p>
<div>
<pre><code><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;EdwinTh/padr&quot;</span><span>)</span><span> </span><span># download the dev version
</span><span>library</span><span>(</span><span>padr</span><span>)</span><span>
</span><span>span_time</span><span>(</span><span>&quot;20170725 00&quot;</span><span>,</span><span> </span><span>&quot;20170725 03&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2017-07-25 00:00:00 UTC&quot; &quot;2017-07-25 01:00:00 UTC&quot;
## [3] &quot;2017-07-25 02:00:00 UTC&quot; &quot;2017-07-25 03:00:00 UTC&quot;
</code></pre>
</div>
<p>We can simplify this even further, specifying the 00 for the hour in <code>from</code> is not strictly necesarry. Since the hour is specified in <code>to</code> already the <code>interval</code> will remain hour if we omit it.</p>
<div>
<pre><code><span>span_time</span><span>(</span><span>&quot;20170725&quot;</span><span>,</span><span> </span><span>&quot;20170725 03&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2017-07-25 00:00:00 UTC&quot; &quot;2017-07-25 01:00:00 UTC&quot;
## [3] &quot;2017-07-25 02:00:00 UTC&quot; &quot;2017-07-25 03:00:00 UTC&quot;
</code></pre>
</div>
<p>We can even use an integer instead of a character for <code>from</code>. When there are no time parts involved, a character is not necesarry. Since we use it in <code>span_time</code> it will be parsed to <code>POSIXct</code>, not to <code>Date</code>.</p>
<div>
<pre><code><span>span_time</span><span>(</span><span>20170725</span><span>,</span><span> </span><span>&quot;20170725 03&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2017-07-25 00:00:00 UTC&quot; &quot;2017-07-25 01:00:00 UTC&quot;
## [3] &quot;2017-07-25 02:00:00 UTC&quot; &quot;2017-07-25 03:00:00 UTC&quot;
</code></pre>
</div>
<p><code>to</code> does not have to be specified, we can use <code>len_out</code> instead. The <code>interval</code> is derived only from <code>from</code> then. To get Jan 1st, from 2010 to 2014 we can do both</p>
<div>
<pre><code><span>span_date</span><span>(</span><span>2010</span><span>,</span><span> </span><span>2014</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2010-01-01&quot; &quot;2011-01-01&quot; &quot;2012-01-01&quot; &quot;2013-01-01&quot; &quot;2014-01-01&quot;
</code></pre>
</div>
<p>and</p>
<div>
<pre><code><span>span_date</span><span>(</span><span>2010</span><span>,</span><span> </span><span>len_out</span><span> </span><span>=</span><span> </span><span>5</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;2010-01-01&quot; &quot;2011-01-01&quot; &quot;2012-01-01&quot; &quot;2013-01-01&quot; &quot;2014-01-01&quot;
</code></pre>
</div>
<p>If you want the <code>interval</code> to be different from the default, you can specify it.</p>
<div>
<pre><code><span>span_date</span><span>(</span><span>2016</span><span>,</span><span> </span><span>2017</span><span>,</span><span> </span><span>interval</span><span> </span><span>=</span><span> </span><span>&quot;month&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;2016-01-01&quot; &quot;2016-02-01&quot; &quot;2016-03-01&quot; &quot;2016-04-01&quot; &quot;2016-05-01&quot;
##  [6] &quot;2016-06-01&quot; &quot;2016-07-01&quot; &quot;2016-08-01&quot; &quot;2016-09-01&quot; &quot;2016-10-01&quot;
## [11] &quot;2016-11-01&quot; &quot;2016-12-01&quot; &quot;2017-01-01&quot;
</code></pre>
</div>
<p>Note however, that you can often also specify the <code>interval</code> by providing more information in <code>from</code> or <code>to</code>.</p>
<div>
<pre><code><span>span_date</span><span>(</span><span>201601</span><span>,</span><span> </span><span>2017</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>##  [1] &quot;2016-01-01&quot; &quot;2016-02-01&quot; &quot;2016-03-01&quot; &quot;2016-04-01&quot; &quot;2016-05-01&quot;
##  [6] &quot;2016-06-01&quot; &quot;2016-07-01&quot; &quot;2016-08-01&quot; &quot;2016-09-01&quot; &quot;2016-10-01&quot;
## [11] &quot;2016-11-01&quot; &quot;2016-12-01&quot; &quot;2017-01-01&quot;
</code></pre>
</div>
<p>I hope you find these little wrappers around <code>seq.Date</code> and <code>seq.POSIXt</code> useful and that they will enable you to conquer dates and datetimes a little quicker. You can obtain the function by downloading the dev version of <code>padr</code> as I did above. If you can think of improvements of the functions before it hits CRAN please tell me. Issues filed, pull requests, emails, and tweets are much appreciated.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://edwinth.github.io/blog/span_date_time/"> That’s so Random</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/3HymSwpq8io" height="1"></a></div>