<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/theory-free-tidyeval/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.johnmackintosh.com/2018-02-19-theory-free-tidyeval/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>	Getting our feet wet with dplyr programming – </p>
<p>I’ve been putting off trying to suss out dplyr’s tidy evaluation for a long time, but I’ve been experimenting a bit with it so just making some notes here.<br>
It’s hard to talk about tidy evaluation / Non Standard Evaluation without getting into a whole load of theory, but I’m going to try it anyway, for the sake of other people like myself who don’t use R all the time.</p>
<p>There are people way smarter than me who’ve written great articles on tidy evaluation, see <a rel="nofollow" href="https://maraaverick.rbind.io/" target="_blank">Mara’s</a> resource list <a rel="nofollow" href="https://maraaverick.rbind.io/2017/08/tidyeval-resource-roundup/" target="_blank">here.</a> You should definitely check those out for more in-depth understanding, if you want to really get under the hood.</p>
<p>Personally, I thought the original programming with dplyr vignette started off OK, but quickly went from “ a pleasant paddle in the sea” to “here be krakens”.</p>
<p>So my aim is to keep this firmly in the range where you can take your flip flops off and roll up your trouser legs, without getting soaked.</p>
<p>Disclaimer – there are things I know about, and there are things I don’t know too much about – and this is in the latter camp. If you see anything wildly innaccurate or misleading, please let me know.. thanks <img alt="🙂" src="https://s.w.org/images/core/emoji/2.3/72x72/1f642.png" class="wp-smiley"></p>
<p>What’s the deal with NSE?<br>
Basically, it allows you to create flexible dplyr functions, to operate on column names which you don’t know yet know the names of.</p>
<p>There are other reasons too – but for me, it the “write once, use many times” flexibility that is a key strength.</p>
<p>Most of the easier examples you see will show you how to group by a variable, and perhaps mutate a new column using another varible.</p>
<p>I’m using the mtcars and flights datasets, which most (if not all) R users will have access to show a few examples.</p>
<p>These mirror the sort of thing I need to do in my work – they might not be massively appropriate for the mtcars and flights data – but you can try them, see what they do, and adapt to your data.</p>
<h1>Grouping on one variable, filtering, mutating, then arranging by multiple variables</h1>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>new_median_rows</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>,</span><span>groupvar</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>nrows</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>...</span><span>)</span><span> </span><span>{</span><span>
</span><span>sortvars</span><span> </span><span>&lt;-</span><span> </span><span>quos</span><span>(</span><span>...</span><span>)</span><span>
</span><span>x_quo</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>x</span><span>)</span><span>
</span><span>groupvar_quo</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>groupvar</span><span>)</span><span>
</span><span>df</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>!!!</span><span>sortvars</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>!!</span><span>groupvar_quo</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>row_number</span><span>(</span><span>!!</span><span>x_quo</span><span>)</span><span> </span><span>%in%</span><span> </span><span>1</span><span>:</span><span>nrows</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Baseline</span><span> </span><span>=</span><span> </span><span>median</span><span>(</span><span>!!</span><span>x_quo</span><span>))</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
</div>
<p>Let’s test it out :</p>
<div>
<div>
<pre><code><span>test</span><span> </span><span>&lt;-</span><span> </span><span>new_median_rows</span><span>(</span><span>df</span><span> </span><span>=</span><span> </span><span>mtcars</span><span>,</span><span>
</span><span>groupvar</span><span> </span><span>=</span><span> </span><span>cyl</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>mpg</span><span>,</span><span>
</span><span>nrows</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span>
</span><span>gear</span><span>,</span><span> </span><span>carb</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>The good news is, this works (or at least, we get some output instead of a bunch of error messages)</p>
<p>What is this function even doing?</p>
<p>We want to group a dataframe by a variable, grab the first 5 rows in each group, calculate a new variable ( taking the median value of mpg, in this example), and then we want to sort the output by 2 further variables.</p>
<p>Let start with the easy bits.<br>
I am simplifying things here probably, but if you are doing any sort of operation with a single variable, then you need to :</p>
<ul>
<li>define it in the function using “enquo”</li>
<li>then bang bang it (using !!)</li>
</ul>
<p>Why? well that’s getting into theory and we don’t want to get our trousers wet.<br>
Oh OK then – it has to do with quoting and unquoting the variables, in order that you can pass “bare” variable names to your function.<br>
(which is why we can write x = mpg, and not x = “mpg” when we actually try it out).</p>
<p>Just accept that you will need to do this, hence my x variable becomes:<br>
x_quo &lt;-enquo(x)<br>
then<br>
median(!!x_quo) in the later part of the function</p>
<p>The same applies when grouping using the “groupvar_quo” variable, and also filtering out the first few rows.</p>
<p>What about those “sortvars”.<br>
What are they? They’re not defined in the function.<br>
Actually – they are – in those 3 little dots at the end.</p>
<p>So when we tested the function with</p>
<div>
<div>
<pre><code><span>test</span><span> </span><span>&lt;-</span><span> </span><span>new_median_rows</span><span>(</span><span>df</span><span> </span><span>=</span><span> </span><span>mtcars</span><span>,</span><span>
</span><span>groupvar</span><span> </span><span>=</span><span> </span><span>cyl</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>mpg</span><span>,</span><span>
</span><span>nrows</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span>
</span><span>gear</span><span>,</span><span> </span><span>carb</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>We asigned variables to all the named arguments,and the gear and carb – which look like they are hanging around at the back and up to no good, are actually passed to sortvars.</p>
<p>You can’t bang bang those though, they need ,er , tripple bangs? (Is that a thing?)<br>
Anyway – you need to use 3 exclamation marks.</p>
<h2>super simple takeaway</h2>
<p>one variable = !!<br>
2 or more variables = … = !!!</p>
<p>This is already way more than I’d intended to write</p>
<p>There are a few more examples below, they are all variations on the same theme, using most of the dplyr verbs.</p>
<p>Here’s one more example – grouping with more than one variable:</p>
<div>
<div>
<pre><code><span>new_median_rows3</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>,</span><span>sortvar</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>nrows</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>...</span><span>)</span><span> </span><span>{</span><span>
</span><span>groupvars</span><span> </span><span>&lt;-</span><span> </span><span>quos</span><span>(</span><span>...</span><span>)</span><span>
</span><span>x_quo</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>x</span><span>)</span><span>
</span><span>sortvar_quo</span><span> </span><span>&lt;-</span><span> </span><span>enquo</span><span>(</span><span>sortvar</span><span>)</span><span>
</span><span>df</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>!!!</span><span>groupvars</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>row_number</span><span>(</span><span>!!</span><span>x_quo</span><span>)</span><span> </span><span>%in%</span><span> </span><span>1</span><span>:</span><span>nrows</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Baseline</span><span> </span><span>=</span><span> </span><span>median</span><span>(</span><span>!!</span><span>x_quo</span><span>))</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>flight_test2</span><span> </span><span>&lt;-</span><span> </span><span>new_median_rows3</span><span>(</span><span>flights</span><span>,</span><span>sortvar</span><span> </span><span>=</span><span> </span><span>dep_time</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>arr_delay</span><span>,</span><span>
</span><span>nrows</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span>
</span><span>origin</span><span>,</span><span>carrier</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>Again – these are perhaps not the best data manipulation examples for these particular datasets but you should be able to suss out what’s going on and adapt these to your needs:</p>
<p></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://www.johnmackintosh.com/2018-02-19-theory-free-tidyeval/"> HighlandR</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/3sRm-j00U3o" height="1"></a></div>