<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/using-r-when-using-do-in-dplyr-dont-forget-the-dot/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://martinsbioblogg.wordpress.com/2017/05/21/using-r-when-using-do-in-dplyr-dont-forget-the-dot/"> R – On unicorns and genes</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>There will be a few posts about switching from plyr/reshape2 for data wrangling to the more contemporary dplyr/tidyr.</p>
<p>My most common use of plyr looked something like this: we take a data frame, split it by some column(s), and use an anonymous function to do something useful. The function takes a data frame and returns another data frame, both of which could very possibly have only one row. (If, in fact, it has to have only one row, I’d suggest an assert_that() call as the first line of the function.)</p>
<pre>
library(plyr)
results &lt;- ddply(some_data, &quot;key&quot;, function(x) {
  ## do something; return data.frame()
})
</pre>
<p>Or maybe, if I felt serious and thought the function would ever be used again, I’d write:</p>
<pre>
calculate &lt;- function(x) {
  ## do something; return data.frame()
}
result &lt;- ddply(some_data, &quot;key&quot;, calculate)
</pre>
<p>Rinse and repeat over and over again. For me, discovering ddply was like discovering vectorization, but for data frames. Vectorization lets you think of operations on vectors, without having to think about their elements. ddply lets you think about operations on data frames, without having to think about rows and columns. It saves a lot of thinking.</p>
<p>The dplyr equivalent would be do(). It looks like this:</p>
<pre>
library(dplyr)
grouped &lt;- group_by(some_data, key)
result &lt;- do(grouped, calculate(.))
</pre>
<p>Or once again with magrittr:</p>
<pre>
library(magrittr)
some_data %>%
  group_by(key) %>%
  do(calculate(.)) -> result
</pre>
<p>(Yes, I used the assignment arrow from the left hand side to the right hand side. Roll your eyes all you want. I think it’s in keeping with the magrittr theme of reading from left to right.)</p>
<p>One important thing here, which got me at first: There has to be a dot! Just passing the function name, as one would have done with ddply, will not work:</p>
<pre>
grouped &lt;- group_by(some_data, key)
## will not work: Error: Results are not data frames at positions ...
try(result &lt;- do(grouped, calculate))
</pre>
<p>Don’t forget the dot!</p>
<p>Postat i:<a rel="nofollow" href="https://martinsbioblogg.wordpress.com/category/computer-stuff/" target="_blank">computer stuff</a> Tagged: <a rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/dplyr/" target="_blank">dplyr</a>, <a rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/plyr/" target="_blank">plyr</a>, <a rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/r/" target="_blank">R</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/martinsbioblogg.wordpress.com/5196/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/delicious/martinsbioblogg.wordpress.com/5196/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/martinsbioblogg.wordpress.com/5196/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/facebook/martinsbioblogg.wordpress.com/5196/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/martinsbioblogg.wordpress.com/5196/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/twitter/martinsbioblogg.wordpress.com/5196/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/martinsbioblogg.wordpress.com/5196/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/stumble/martinsbioblogg.wordpress.com/5196/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/martinsbioblogg.wordpress.com/5196/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/digg/martinsbioblogg.wordpress.com/5196/"></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/martinsbioblogg.wordpress.com/5196/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/reddit/martinsbioblogg.wordpress.com/5196/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></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://martinsbioblogg.wordpress.com/2017/05/21/using-r-when-using-do-in-dplyr-dont-forget-the-dot/"> R – On unicorns and genes</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/w3uNDUCqSNU" height="1"></a></div>