<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/i-fell-out-with-tapply-and-in-love-with-dplyr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://scottishsnow.wordpress.com/2018/10/15/fell-out-tapply-love-dplyr/"> R – scottishsnow</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A long time ago (5 years) I wrote a <a rel="nofollow" href="https://scottishsnow.wordpress.com/2013/10/11/tapply/" target="_blank">blog post on tapply</a>. Back then I was just getting into programming and I thought the possibilities of tapply were amazing. So it seems, do many others as it’s become one of my most viewed articles.</p>
<p>However, I never use tapply these days because the output is either a named vector or a matrix. Both of these require munging if I’m going to use the output. Three months after I wrote my tapply post a little <a rel="nofollow" href="https://blog.rstudio.com/2014/01/17/introducing-dplyr/" target="_blank">package called dplyr was released</a>. It took a while before it became integral to my workflow (I like to use as few packages as possible), but now I use it almost daily. The two biggest reasons are:</p>
<ol>
<li>A data frame as output</li>
<li>Readable code.</li>
</ol>
<p>Now we’re all living in the <a rel="nofollow" href="https://www.tidyverse.org/" target="_blank">tidyverse</a>, I’m a bit confused that so many folk still land on my blog looking for tapply. So this post updates/supersedes what I wrote previously. I’ve repeated the toy example I made before:</p>
<pre>
# Generate an example time series
df = data.frame(date=seq.Date(as.Date(&quot;1990-01-01&quot;),
                              as.Date(&quot;2013-12-31&quot;),
                              by=1))

# Add some data (0s and 1s)
df = df %>%
   mutate(snow_lying=sample(c(0, 1), nrow(df), replace=T))

# Get month and year from date
df = df %>%
   mutate(month=format(date, &quot;%m&quot;),
          year=format(date, &quot;%Y&quot;))

# Sum for each month
df %>%
   group_by(month) %>%
   summarise(snow_days=sum(snow_lying))

# Sum for each month, each year
df %>%
   group_by(year, month) %>%
   summarise(snow_days=sum(snow_lying))
</pre>
<div data-shortcode="caption" class="wp-caption"><a rel="nofollow" href="https://dplyr.tidyverse.org/" target="_blank"><img data-orig-file="https://scottishsnow.files.wordpress.com/2018/10/hex-dplyr.png?w=456" data-orig-size="736,853" data-medium-file="https://scottishsnow.files.wordpress.com/2018/10/hex-dplyr.png?w=456?w=259" data-attachment-id="1588" data-image-meta="{" data-image-description="" src="https://scottishsnow.files.wordpress.com/2018/10/hex-dplyr.png?w=456" data-permalink="https://scottishsnow.wordpress.com/2018/10/15/fell-out-tapply-love-dplyr/hex-dplyr/" data-large-file="https://scottishsnow.files.wordpress.com/2018/10/hex-dplyr.png?w=456?w=620" alt="hex-dplyr" data-image-title="hex-dplyr" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-1588" data-recalc-dims="1"></a>
<p class="wp-caption-text">Read more about dplyr here: <a rel="nofollow" href="https://dplyr.tidyverse.org/" target="_blank">https://dplyr.tidyverse.org/</a></p>
</div>
	<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://scottishsnow.wordpress.com/2018/10/15/fell-out-tapply-love-dplyr/"> R – scottishsnow</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ZNep_SIUN-s" height="1"></a></div>