<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/some-neat-new-r-notations/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2017/08/some-neat-new-r-notations/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <a rel="nofollow" href="https://www.r-project.org/" target="_blank"><code>R</code></a> package <a rel="nofollow" href="https://cran.r-project.org/package=seplyr" target="_blank"><code>seplyr</code></a> supplies a few neat new coding notations.</p>
<p><center><br>
<a rel="nofollow" href="https://en.wikipedia.org/wiki/Abacus" target="_blank"><img border="0" alt="abacus" title="NewImage.png" src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/08/NewImage-1.png?w=660" data-recalc-dims="1">
<p>
An Abacus, which gives us the term “calculus.”<br>
</a></center><span></span></p>
<p>
<p>The first notation is an operator called the <a rel="nofollow" href="https://winvector.github.io/seplyr/articles/named_map_builder.html" target="_blank">“named map builder”</a>. This is a cute notation that essentially does the job of <code>stats::setNames()</code>. It allows for code such as the following:</p>
<pre>
library(&quot;seplyr&quot;)

names &lt;- c('a', 'b')

names := c('x', 'y')
#>   a   b 
#> &quot;x&quot; &quot;y&quot;
</pre>
<p>This can be <em>very</em> useful when programming in <code>R</code>, as it allows indirection or abstraction on the left-hand side of inline name assignments (unlike <code>c(a = 'x', b = 'y')</code>, where all left-hand-sides are concrete values even if not quoted).</p>
<p>A nifty property of the named map builder is it <a rel="nofollow" href="https://en.wikipedia.org/wiki/Commutative_diagram" target="_blank">commutes</a> (in the sense of algebra or category theory) with <code>R</code>‘s “<code>c()</code>” combine/concatenate function. That is: <code>c('a' := 'x', 'b' := 'y')</code> is the same as <code>c('a', 'b') := c('x', 'y')</code>. Roughly this means the two operations play well with each other.</p>
<p>The second notation is an operator called “<a rel="nofollow" href="https://winvector.github.io/seplyr/reference/makeFunction_se.html" target="_blank">anonymous function builder</a>“. For technical reasons we use the same “<code>:=</code>” notation for this (and, as is common in <code>R</code>, pick the correct behavior based on runtime types).</p>
<p>The function construction is written as: “<code>variables := { code }</code>” (the braces are required) and the semantics are roughly the same as “<code>function(variables) { code }</code>“. This is derived from some of the work of <a rel="nofollow" href="http://klmr.me/about/" target="_blank">Konrad Rudolph</a> who noted that most functional languages have a more concise “lambda syntax” than “function(){}” (please see <a rel="nofollow" href="http://www.win-vector.com/blog/2016/12/the-case-for-using-in-r/comment-page-1/#comment-66399" target="_blank">here</a> and <a rel="nofollow" href="https://github.com/klmr/functional#a-concise-lambda-syntax" target="_blank">here</a> for some details, and be aware the <code>seplyr</code> notation is not as concise as is possible).</p>
<p>This notation allows us to write the squares of <code>1</code> through <code>4</code> as:</p>
<pre>
sapply(1:4, x:={x^2})
</pre>
<p>instead of writing:</p>
<pre>
sapply(1:4, function(x) x^2)
</pre>
<p>It is only a few characters of savings, but being able to choose notation <a rel="nofollow" href="http://dl.acm.org/citation.cfm?id=358899" target="_blank">can be a big deal</a>. A real victory would be able to directly use <a rel="nofollow" href="https://en.wikipedia.org/wiki/Lambda_calculus" target="_blank">lambda-calculus</a> notation such as “<code>(λx.x^2)</code>“. In the <a rel="nofollow" href="https://github.com/WinVector/seplyr" target="_blank">development version of <code>seplyr</code></a> we are experimenting with the following additional notations:</p>
<pre>
sapply(1:4, lambda(x)(x^2))
</pre>
<pre>
sapply(1:4, λ(x, x^2))
</pre>
<p>(Both of these currenlty work in the development version, though we are not sure about submitting source files with non-ASCII characters to CRAN.)</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="http://www.win-vector.com/blog/2017/08/some-neat-new-r-notations/"> R – Win-Vector Blog</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/a4g7PBztgAs" height="1"></a></div>