<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/its-tibbletime-time-aware-tibbles/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/code-tools/2017/09/07/tibbletime-0-0-1.html"> business-science.io - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We are very excited to announce the initial release of our newest R package,<br>
<code>tibbletime</code>. As evident from the name, <code>tibbletime</code> is built on top of the<br>
<code>tibble</code> package (and more generally on top of the <code>tidyverse</code>) with the main<br>
purpose of being able to create <strong>time-aware tibbles</strong> through a one-time<br>
specification of an “index” column (a column containing timestamp information). There are a ton of useful time functions that we can now use such as <code>time_filter()</code>, <code>time_summarize()</code>, <code>tmap()</code>, <code>as_period()</code> and <code>time_collapse()</code>. We’ll walk through the basics in this post.</p>
<p>If you like what we do, please <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/09/07/tibbletime-0-0-1.html#social" target="_blank">follow us on social media</a> to stay up on the latest <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/09/07/tibbletime-0-0-1.html#contact" target="_blank">Business Science</a> news, events and information! As always, we are interested in both expanding our <em>network of data scientists</em> and seeking <em>new clients interested in applying data science to business and finance</em>. If interested, <a rel="nofollow" href="http://www.business-science.io/contact.html" target="_blank">contact us</a>.</p>
<h2>Why tibbletime?</h2>
<p>The tidyverse has some great packages such as <code>dplyr</code>, <code>tidyr</code>, and <code>purrr</code> that really make data science in R both easier and fun. However, none of these packages specifically address time series. Often R users will find themselves continuously switching to and from <code>xts</code> to leverage the benefits of time-based functions and operations such as grouping, applying functions, summarizing and so on all by time.</p>
<p>Enter <code>tibbletime</code>, a new class that is <strong>time-aware</strong>. Advantages of this new class are:</p>
<ul>
<li>
<p>The ability to perform compact, time-based subsetting on tibbles.</p>
</li>
<li>
<p>Quickly summarisize and aggregate results by time period (yearly, monthly, etc).</p>
</li>
<li>
<p>Change the periodicity of a tibble, meaning from a daily dataset to a monthly or yearly dataset with one function call.</p>
</li>
<li>
<p>Call functions similar in spirit to the <code>map</code> family from <code>purrr</code> on time based tibbles.</p>
</li>
</ul>
<p>In addition to these new abilities, all functions were designed to support the pipe (<code>%>%</code>) and to work seamlessly with “tidyverse” packages like <code>dplyr</code>, <code>tidyr</code> and <code>purrr</code>. The end result is a new set of tools that make time series in the “tidyverse” much easier and more productive.</p>
<h2>Libraries Needed</h2>
<p>To get started, load <code>tibbletime</code>. You can download from CRAN or from GitHub.</p>
<figure>
<pre><code data-lang="r"><span># Install from either CRAN or GH
# install.packages(&quot;tibbletime&quot;)
# devtools::install_github(&quot;business-science/tibbletime&quot;)
</span><span>
</span><span># Load libraries
</span><span>library</span><span>(</span><span>tibbletime</span><span>)</span></code></pre>
</figure>
<h2>Creating time-based tibbles</h2>
<p>To get started with <code>tibbletime</code>, you’ll use <code>as_tbl_time()</code> to transform<br>
your <code>tibble</code> into a <code>tbl_time</code> object. Of course, you’ll need a column of<br>
dates to use as your index.</p>
<p>Below, we’ll use the <code>FB</code> data set included with the package (Facebook stock<br>
prices).</p>
<figure>
<pre><code data-lang="r"><span># Facebook stock prices. Comes with the package
</span><span>data</span><span>(</span><span>FB</span><span>)</span></code></pre>
</figure>
<p>We start with a <code>tibble</code>. Notice the specification of the <code>index</code> argument as the <code>date</code> column of <code>FB</code>.</p>
<figure>
<pre><code data-lang="r"><span># Originally a tibble
</span><span>FB</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 1,008 x 8
##    symbol       date  open  high   low close    volume adjusted
##     </code></pre></figure>