<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/tidy-time-series-analysis-part-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/timeseries-analysis/2017/07/02/tidy-timeseries-analysis.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>In the first part in a series on <strong>Tidy Time Series Analysis</strong>, we’ll use <code>tidyquant</code> to investigate CRAN downloads. You’re probably thinking, “Why tidyquant?” Most people think of <code>tidyquant</code> as purely a financial package and rightfully so. However, because of its integration with <code>xts</code>, <code>zoo</code> and <code>TTR</code>, it’s naturally suited for <strong>“tidy” time series analysis</strong>. In this post, we’ll discuss the the “period apply” functions from the <code>xts</code> package, which make it easy to apply functions to time intervals in a <strong>“tidy”</strong> way using <code>tq_transmute()</code>!</p>
<p>An example of the visualization we can create using the period apply functions with <code>tq_transmute()</code>:</p>
<p><img alt="Median tidyverse downloads by week" src="https://i2.wp.com/www.business-science.io/assets/tidy-timeseries-part1.png?w=456" data-recalc-dims="1"></p>
<h1>Libraries Needed</h1>
<p>We’ll primarily be using two libraries today.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span> </span><span># Loads tidyverse, tidquant, financial pkgs, xts/zoo
</span><span>library</span><span>(</span><span>cranlogs</span><span>)</span><span> </span><span>#</span><span> </span><span>For</span><span> </span><span>inspecting</span><span> </span><span>package</span><span> </span><span>downloads</span><span> </span><span>over</span><span> </span><span>time</span></code></pre>
</figure>
<h1>CRAN tidyverse Downloads</h1>
<p>As you can tell from my laptop stickers, I’m a bit of a <code>tidyverse</code> fan. <img alt="🙂" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png" class="wp-smiley"> The packages are super useful so it’s no wonder why several of these packages rank in the top downloads according to <a rel="nofollow" href="https://www.rdocumentation.org/trends?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">RDocumenation.org’s Leaderboard</a> by DataCamp.</p>
<p><img alt="Stickers" src="https://i0.wp.com/www.business-science.io/assets/stickers.png?w=456" data-recalc-dims="1"></p>
<p><center><em>We love the tidyverse!</em></center></p>
<p></p>
<p>A good way to inspect the trends in popularity with these packages is to examine the CRAN downloads. So how do we get download data? The <code>cranlogs</code> package has a convenient function, <code>cran_downloads()</code>, that allows us to retrieve daily downloads of various packages. Getting downloads is as easy as making a vector of the packages we want to analyze and using <code>cran_downloads()</code>. I’ve added a date range over the past six months since <code>tidyquant</code> has only been in existence since then.</p>
<figure>
<pre><code data-lang="r"><span># Various tidyverse packages corresponding to my stickers :)
</span><span>pkgs</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>
</span><span>&quot;tidyr&quot;</span><span>,</span><span> </span><span>&quot;lubridate&quot;</span><span>,</span><span> </span><span>&quot;dplyr&quot;</span><span>,</span><span> </span><span>&quot;broom&quot;</span><span>,</span><span> </span><span>&quot;tidyquant&quot;</span><span>,</span><span> </span><span>&quot;ggplot2&quot;</span><span>,</span><span> </span><span>&quot;purrr&quot;</span><span>,</span><span> </span><span>&quot;stringr&quot;</span><span>,</span><span> </span><span>&quot;knitr&quot;</span><span>
</span><span>)</span><span>
</span><span># Get the downloads for the individual packages
</span><span>tidyverse_downloads</span><span> </span><span>&lt;-</span><span> </span><span>cran_downloads</span><span>(</span><span>
</span><span>packages</span><span> </span><span>=</span><span> </span><span>pkgs</span><span>,</span><span> </span><span>from</span><span> </span><span>=</span><span> </span><span>&quot;2017-01-01&quot;</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>&quot;2017-06-30&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>tibble</span><span>::</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>package</span><span>)</span><span>
</span><span>tidyverse_downloads</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 1,629 x 3
## # Groups:   package [9]
##          date count package
##  *     </code></pre></figure>