<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/padrpad-does-now-do-group-padding/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://edwinth.github.io/blog/pad.v0.2.0/"> That’s so Random</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A few weeks ago <code>padr</code> was introduced on CRAN, allowing you to quickly get datetime data ready for analysis. If you have missed this, see <a target="_blank" rel="nofollow" href="https://edwinth.github.io/blog/padr-intro/">the introduction blog</a> or <code>vignette(&quot;padr&quot;)</code> for a general introduction. In v0.2.0 the <code>pad</code> function is extended with a <code>group</code> argument, which makes your life a lot easier when you want to do padding within groups.</p>
<p>In the Examples of <code>padr</code> in v0.1.0 I showed that padding over multiple groups could be done by using <code>padr</code> in conjunction with <code>dplyr</code> and <code>tidyr</code>.</p>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>padr</span><span>)</span><span>
</span><span># padding a data.frame on group level
</span><span>day_var</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>as.Date</span><span>(</span><span>'2016-01-01'</span><span>),</span><span> </span><span>length.out</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>'month'</span><span>)</span><span>
</span><span>x_df_grp</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>grp</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>LETTERS</span><span>[</span><span>1</span><span>:</span><span>3</span><span>],</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>4</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>runif</span><span>(</span><span>12</span><span>,</span><span> </span><span>10</span><span>,</span><span> </span><span>20</span><span>)</span><span> </span><span>%>%</span><span> </span><span>round</span><span>(</span><span>0</span><span>),</span><span>
</span><span>date</span><span> </span><span>=</span><span> </span><span>sample</span><span>(</span><span>day_var</span><span>,</span><span> </span><span>12</span><span>,</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>grp</span><span>,</span><span> </span><span>date</span><span>)</span><span>
</span><span>x_df_grp</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>grp</span><span>)</span><span> </span><span>%>%</span><span> </span><span>do</span><span>(</span><span>pad</span><span>(</span><span>.</span><span>))</span><span> </span><span>%>%</span><span> </span><span>ungroup</span><span> </span><span>%>%</span><span>
</span><span>tidyr</span><span>::</span><span>fill</span><span>(</span><span>grp</span><span>)</span><span> </span></code></pre>
</div>
<p>I quite quickly realized this is an unsatisfactory solution for two reasons:</p>
<p>1) It is a hassle. It is the goal of <code>padr</code> to make datetime preparation as swift and pain free as possible. Having to manually fill your grouping variable(s) after padding is not exactly in concordance with that goal.<br>
2) It does not work when one or both of the <code>start_val</code> and <code>end_val</code> arguments are specified. The start and/or the end of the time series of a group are then no longer bounded by an original observation, and thus don’t have a value of the grouping variable(s). Forward filling with <code>tidyr::fill</code> will incorrectly fill the grouping variable(s) as a result.</p>
<p>It was therefore necessary to expand <code>pad</code>, so the grouping variable(s) do not contain missing values anymore after padding. The <code>group</code> argument takes a character vector with the column name(s) of the variables to group by. Padding will be done on each of the groups formed by the unique combination of the grouping variables. This is of course just the distinct of the variable, if there is only one grouping variable. The result of the date padding will be exactly the same as before this addition (meaning the datetime variable does not change). However, the returned data frame will no longer have missing values for the grouping variables on the padded rows.</p>
<p>The new version of the section in the Examples of <code>padr</code> is:</p>
<div>
<pre><code><span>day_var</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>as.Date</span><span>(</span><span>'2016-01-01'</span><span>),</span><span> </span><span>length.out</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>'month'</span><span>)</span><span>
</span><span>x_df_grp</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>grp1</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>LETTERS</span><span>[</span><span>1</span><span>:</span><span>3</span><span>],</span><span> </span><span>each</span><span> </span><span>=</span><span>4</span><span>),</span><span>
</span><span>grp2</span><span> </span><span>=</span><span> </span><span>letters</span><span>[</span><span>1</span><span>:</span><span>2</span><span>],</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>runif</span><span>(</span><span>12</span><span>,</span><span> </span><span>10</span><span>,</span><span> </span><span>20</span><span>)</span><span> </span><span>%>%</span><span> </span><span>round</span><span>(</span><span>0</span><span>),</span><span>
</span><span>date</span><span> </span><span>=</span><span> </span><span>sample</span><span>(</span><span>day_var</span><span>,</span><span> </span><span>12</span><span>,</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>grp1</span><span>,</span><span> </span><span>grp2</span><span>,</span><span> </span><span>date</span><span>)</span><span>
</span><span># pad by one grouping var
</span><span>x_df_grp</span><span> </span><span>%>%</span><span> </span><span>pad</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>'grp1'</span><span>)</span><span>
</span><span># pad by two groups vars
</span><span>x_df_grp</span><span> </span><span>%>%</span><span> </span><span>pad</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'grp1'</span><span>,</span><span> </span><span>'grp2'</span><span>))</span><span>
</span></code></pre>
</div>
<h3>Bug fixes</h3>
<p>Besides the additional argument there were two bug fixes in this version:</p>
<ul>
<li>
<p><code>pad</code> does no longer break when datetime variable contains one value only. Returns <code>x</code> and a warning if <code>start_val</code> and <code>end_val</code> are <code>NULL</code>, and will do proper padding when one or both are specified.</p>
</li>
<li>
<p>In the <code>fill_</code> function now a meaningful error is thrown, when forgetting to specify at least one column to apply the function on.</p>
</li>
</ul>
<h3>v0.2.1</h3>
<p>Right before posting this blog, Doug Friedman found out that in v0.2.0 the <code>by</code> argument no longer functioned. This bug was fixed in the patch release v0.2.1.</p>
<p>I hope you (still) enjoy working with <code>padr</code>, <a target="_blank" rel="nofollow" href="https://github.com/edwinth/padr">let me know</a> when you find a bug or got ideas for improvement.</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/pad.v0.2.0/"> 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 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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/JdilKGKi0uI"></a></div>