<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/why-do-we-use-arrow-as-an-assignment-operator/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://colinfay.me/r-assignment/"> Colin Fay</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A <a rel="nofollow" href="https://twitter.com/_ColinFay/status/1006139974377443328" target="_blank">Twitter<br>
Thread</a> turned<br>
into a blog post.</p>
<p><span></span></p>
<p>In June, I published a little<br>
<a rel="nofollow" href="https://twitter.com/_ColinFay/status/1006139974377443328" target="_blank">thread</a> on<br>
Twitter about the history of the <code>&lt;-</code> assignment operator in R. Here is<br>
a blog post version of this thread.</p>
<h2>Historical reasons</h2>
<p>As you all know, R comes from S. But you might not know a lot about S (I<br>
don’t). This language used <code>&lt;-</code> as an assignment operator. It’s partly<br>
because it was inspired by a language called APL, which also had this<br>
sign for assignment.</p>
<p>But why again? APL was designed on a specific keyboard, which had a key<br>
for<br>
<code>&lt;-</code>:</p>
<p><img src="https://i1.wp.com/upload.wikimedia.org/wikipedia/commons/thumb/9/9f/APL-keybd2.svg/410px-APL-keybd2.svg.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>At that time, it was also chosen because there was no <code>==</code> for testing<br>
equality: equality was tested with <code>=</code>, so assigning a variable needed<br>
to be done with another symbol.</p>
<p><img align="center" src="https://i2.wp.com/colinfay.me/assets/img/blog/apl.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<blockquote>
<p>From <a rel="nofollow" href="http://www.softwarepreservation.org/projects/apl/Books/APL360ReferenceManual" target="_blank">APL Reference<br>
Manual</a></p>
</blockquote>
<p>Until <a rel="nofollow" href="http://developer.r-project.org/equalAssign.html" target="_blank">2001</a>, in R, <code>=</code><br>
could only be used for assigning function arguments, like <code>fun(foo =<br>
&quot;bar&quot;)</code> (remember that R was born in 1993). So before 2001, the <code>&lt;-</code> was<br>
the standard (and only way) to assign value into a variable.</p>
<p>Before that, <code>_</code> was also a valid assignment operator. It was removed in<br>
<a rel="nofollow" href="https://cran.r-project.org/src/base/NEWS.1" target="_blank">R 1.8</a>:</p>
<p><img align="center" src="https://i0.wp.com/colinfay.me/assets/img/blog/runderscore.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>(So no, at that time, no snake_case_naming_convention)</p>
<p>Colin Gillespie published <a rel="nofollow" href="https://www.jumpingrivers.com/blog/r-from-the-turn-of-the-century/" target="_blank">some of his code from<br>
early 2000</a>,<br>
where assignment was made like this <img alt="🙂" src="https://s.w.org/images/core/emoji/2.4/72x72/1f642.png" class="wp-smiley"></p>
<p>The main reason “equal assignment” was introduced is because other<br>
languages uses <code>=</code> as an assignment method, and because it increased<br>
compatibility with S-Plus.</p>
<h2>And today?</h2>
<h3>Readability</h3>
<p>Nowadays, there are seldom any cases when you can’t use one in place of<br>
the other. It’s safe to use <code>=</code> almost everywhere. Yet, <code>&lt;-</code> is<br>
preferred and advised in R Coding style guides:</p>
<ul>
<li><a rel="nofollow" href="https://google.github.io/styleguide/Rguide.xml#assignment" target="_blank">https://google.github.io/styleguide/Rguide.xml#assignment</a></li>
<li><a rel="nofollow" href="http://adv-r.had.co.nz/Style.html" target="_blank">http://adv-r.had.co.nz/Style.html</a></li>
</ul>
<p>One reason, if not historical, to prefer the <code>&lt;-</code> is that it clearly<br>
states in which side you are making the assignment (you can assign from<br>
left to right or from right to left in R):</p>
<div>
<div>
<pre><code><span>a</span><span> </span><span>&lt;-</span><span> </span><span>12</span><span>
</span><span>13</span><span> </span><span>-></span><span> </span><span>b</span><span> </span><span>a</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 12
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>b</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 13
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>a</span><span> </span><span>-></span><span> </span><span>b</span><span>
</span><span>a</span><span> </span><span>&lt;-</span><span> </span><span>b</span><span>
</span></code></pre>
</div>
</div>
<p>The RHS assignment can for example be used for assigning the <a rel="nofollow" href="https://rud.is/b/2015/02/04/a-step-to-the-right-in-r-assignments/" target="_blank">result of<br>
a<br>
pipe</a></p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>iris</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Species</span><span> </span><span>==</span><span> </span><span>&quot;setosa&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>-</span><span>Species</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise_all</span><span>(</span><span>mean</span><span>)</span><span> </span><span>-></span><span> </span><span>res</span><span>
</span><span>res</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1        5.006       3.428        1.462       0.246
</code></pre>
</div>
</div>
<p>Also, it’s easier to distinguish equality comparison and assignment in<br>
the last line of code here:</p>
<div>
<div>
<pre><code><span>c</span><span> </span><span>&lt;-</span><span> </span><span>12</span><span>
</span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>13</span><span>
</span><span>e</span><span> </span><span>=</span><span> </span><span>c</span><span> </span><span>==</span><span> </span><span>d</span><span>
</span><span>f</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span> </span><span>==</span><span> </span><span>d</span><span>
</span></code></pre>
</div>
</div>
<p>Note that <code>&lt;-</code> and <code>->></code> also exist:</p>
<div>
<div>
<pre><code><span>create_plop_pouet</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>a</span><span>,</span><span> </span><span>b</span><span>){</span><span>
</span><span>plop</span><span> </span><span>&lt;-</span><span> </span><span>a</span><span>
</span><span>b</span><span> </span><span>->></span><span> </span><span>pouet</span><span>
</span><span>}</span><span>
</span><span>create_plop_pouet</span><span>(</span><span>4</span><span>,</span><span> </span><span>5</span><span>)</span><span>
</span><span>plop</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 4
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>pouet</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 5
</code></pre>
</div>
</div>
<p>And that Ross Ihaka uses <code>=</code> :<br>
<a rel="nofollow" href="https://www.stat.auckland.ac.nz/~ihaka/downloads/JSM-2010.pdf" target="_blank">https://www.stat.auckland.ac.nz/~ihaka/downloads/JSM-2010.pdf</a></p>
<h3>Environments</h3>
<p>There are some environment and precedence differences. For example,<br>
assignment with <code>=</code> is only done on a functional level, whereas <code>&lt;-</code><br>
does it on the top level when called inside as a function argument.</p>
<div>
<div>
<pre><code><span>median</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>10</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 5.5
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>x</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Error in eval(expr, envir, enclos): objet 'x' introuvable
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>median</span><span>(</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>1</span><span>:</span><span>10</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 5.5
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>x</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##  [1]  1  2  3  4  5  6  7  8  9 10
</code></pre>
</div>
</div>
<p>In the first code, you’re passing <code>x</code> as the parameter of the <code>median</code><br>
function, whereas the second one is creating a variable x in the<br>
environment, and uses it as the first argument of <code>median</code>. Note that it<br>
works because <code>x</code> is the name of the parameter of the function, and<br>
won’t work with<br>
<code>y</code>:</p>
<div>
<div>
<pre><code><span>median</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>12</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Error in is.factor(x): l'argument &quot;x&quot; est manquant, avec aucune valeur par défaut
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>median</span><span>(</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>12</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 12
</code></pre>
</div>
</div>
<p>There is also a difference in parsing when it comes to both these<br>
operators (but I guess this never happens in the real world), one<br>
failing and not the other:</p>
<div>
<div>
<pre><code><span>x</span><span> </span><span>&lt;-</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>15</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Error in x &lt;- y = 15: impossible de trouver la fonction &quot;&lt;-&lt;-&quot;
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>x</span><span> </span><span>=</span><span> </span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>15</span><span>
</span><span>c</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 15 15
</code></pre>
</div>
</div>
<p>It is also good practice because it clearly indicates the difference<br>
between function arguments and assignation:</p>
<div>
<div>
<pre><code><span>x</span><span> </span><span>&lt;-</span><span> </span><span>shapiro.test</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>iris</span><span>$</span><span>Sepal.Length</span><span>)</span><span>
</span><span>x</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## 
##  Shapiro-Wilk normality test
## 
## data:  iris$Sepal.Length
## W = 0.97609, p-value = 0.01018
</code></pre>
</div>
</div>
<p>And this weird behavior:</p>
<div>
<div>
<pre><code><span>rm</span><span>(</span><span>list</span><span> </span><span>=</span><span> </span><span>ls</span><span>())</span><span>
</span><span>data.frame</span><span>(</span><span>
</span><span>a</span><span> </span><span>=</span><span> </span><span>rnorm</span><span>(</span><span>10</span><span>),</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>rnorm</span><span>(</span><span>10</span><span>)</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##             a b....rnorm.10.
## 1   0.9885196      1.3809205
## 2  -0.2810080     -1.4165648
## 3  -0.6709831     -1.6203407
## 4  -1.3055656     -1.0713406
## 5   1.2297421      2.2558878
## 6  -1.5333307      0.5194378
## 7  -0.1011028     -0.3651725
## 8  -0.3976268     -1.0814520
## 9  -0.3924576     -0.7030822
## 10 -1.1745994     -0.7090015
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>a</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Error in eval(expr, envir, enclos): objet 'a' introuvable
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>b</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##  [1]  1.3809205 -1.4165648 -1.6203407 -1.0713406  2.2558878  0.5194378
##  [7] -0.3651725 -1.0814520 -0.7030822 -0.7090015
</code></pre>
</div>
</div>
<h2>Little bit unrelated but</h2>
<p>I love this one:</p>
<div>
<div>
<pre><code><span>g</span><span> </span><span>&lt;-</span><span> </span><span>12</span><span> </span><span>-></span><span> </span><span>h</span><span>
</span><span>g</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 12
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>h</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 12
</code></pre>
</div>
</div>
<p>Which of course is not doable with <code>=</code>.</p>
<h2>Other operators</h2>
<p>Some users pointed out on Twitter that this could make the code a little<br>
bit harder to read if you come from another language. <code>&lt;-</code> is use “only”<br>
use in F#, OCaml, R and S (as far as Wikipedia can tell). Even if <code>&lt;-</code><br>
is rare in programming, I guess its meaning is quite easy to grasp,<br>
though.</p>
<p>Note that the second most used assignment operator is <code>:=</code> (<code>=</code> being<br>
the most common). It’s used in <code>{data.table}</code> and <code>{rlang}</code> notably. The<br>
<code>:=</code> operator is not defined in the current R language, but has not been<br>
removed, and is still understood by the R parser. You can’t use it on<br>
the top level:</p>
<div>
<div>
<pre><code><span>a</span><span> </span><span>:=</span><span> </span><span>12</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Error in `:=`(a, 12): impossible de trouver la fonction &quot;:=&quot;
</code></pre>
</div>
</div>
<p>But as it is still understood by the parser, you can use <code>:=</code> as an<br>
infix without any %%, for assignment, or for anything else:</p>
<div>
<div>
<pre><code><span>`:=`</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>){</span><span>
</span><span>x</span><span>$</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span>x</span><span>
</span><span>}</span><span>
</span><span>head</span><span>(</span><span>iris</span><span> </span><span>:=</span><span> </span><span>Sepal.Length</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
</code></pre>
</div>
</div>
<p>You can see that <code>:=</code> was used as an assignment operator<br>
<a rel="nofollow" href="https://developer.r-project.org/equalAssign.html" target="_blank">https://developer.r-project.org/equalAssign.html</a> :</p>
<blockquote>
<p>All the previously allowed assignment operators (&lt;-, :=, _, and<br>
&lt;-) remain fully in effect</p>
</blockquote>
<p>Or in R NEWS 1:</p>
<p><img align="center" src="https://i2.wp.com/colinfay.me/assets/img/blog/colonequal.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>See also</h2>
<ul>
<li>Around 29’:<br>
<a rel="nofollow" href="https://channel9.msdn.com/Events/useR-international-R-User-conference/useR2016/Forty-years-of-S" target="_blank">https://channel9.msdn.com/Events/useR-international-R-User-conference/useR2016/Forty-years-of-S</a></li>
<li><a rel="nofollow" href="http://blog.revolutionanalytics.com/2008/12/use-equals-or-arrow-for-assignment.html" target="_blank">Use = or &lt;-for<br>
assignment?</a></li>
<li><a rel="nofollow" href="https://stackoverflow.com/questions/1741820/what-are-the-differences-between-and-in-r/1742550#1742550" target="_blank">What are the differences between “=” and “&lt;-” in<br>
R?</a></li>
<li><a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/assignOps.html" target="_blank">Assignment<br>
Operators</a></li>
</ul>
	<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://colinfay.me/r-assignment/"> Colin Fay</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/88y2JBJSjgA" height="1"></a></div>