<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/playing-around-with-methods-overloading-c-language-and-operators-1/"></div></div><div><a data-url="http://www.r-bloggers.com/playing-around-with-methods-overloading-c-language-and-operators-1/" data-counturl="http://www.r-bloggers.com/playing-around-with-methods-overloading-c-language-and-operators-1/" data-count="horizontal" data-text="Playing Around with Methods Overloading, C-language and Operators (1)" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.milanor.net/blog/playing-around-methods-overloading-c-language-operators/"> MilanoR</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<div>
<p><em>This post was originally posted on Quantide blog. Read the <a target="_blank" href="http://www.quantide.com/website/playing-around-methods-overloading-c-language-operators/">full article here</a>.</em></p>
<h2>Introduction</h2>
<p>R is an object-oriented (OO) language. This basically means that R is able to recognize the type of objects generate from analysis and to apply the right operation on different objects.</p>
<p>For example, the <code>summary(x)</code> method performs different operations depending on the so-called “class” of <code>x</code>:</p>
<pre>x &lt;- data.frame(first=1:10, second=letters[1:10])
class(x) # An object of class &quot;data.frame&quot;</pre>

<pre>## [1] &quot;data.frame&quot;</pre>

<pre>summary(x)</pre>

<pre>##      first           second 
##  Min.   : 1.00   a      :1  
##  1st Qu.: 3.25   b      :1  
##  Median : 5.50   c      :1  
##  Mean   : 5.50   d      :1  
##  3rd Qu.: 7.75   e      :1  
##  Max.   :10.00   f      :1  
##                  (Other):4</pre>

<pre>ds &lt;- data.frame(x=1:100, y=100 + 3 * 1:100 + rnorm(100,sd = 10))
md &lt;- lm(formula = y ~ x, data = ds)
class(md) # An object of class &quot;lm&quot;</pre>

<pre>## [1] &quot;lm&quot;</pre>

<pre>summary(md)</pre>

<pre>## 
## Call:
## lm(formula = y ~ x, data = ds)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -20.1695  -5.8434  -0.4058   5.3611  27.9861 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 100.07892    1.99555   50.15   &lt;2e-16 ***
## x             2.98890    0.03431   87.12   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.903 on 98 degrees of freedom
## Multiple R-squared:  0.9873, Adjusted R-squared:  0.9871 
## F-statistic:  7590 on 1 and 98 DF,  p-value: &lt; 2.2e-16</pre>
<p>the outputs reported, and the calculations performed, by the <code>summary()</code> method are really different for the <code>x</code> and the <code>md</code>objects.</p>
<p>This behavior is one of characteristics of OO languages, and it is called <em>methods overloading</em>.</p>
<p>The methods overloading can be applied not only for methods in “function form” (i.e., for methods like <code>summary()</code>), but also for operators; indeed, “behind the scenes”, the operators are functions/methods. For example, if we try to write <code><code>+</code></code> in the R console we obtain:</p>
<pre>`+`</pre>

<pre>## function (e1, e2)  .Primitive(&quot;+&quot;)</pre>
<p>That means that the <code>+</code> operator actually is a function/method that requires two arguments: <code>e1</code> and <code>e2</code>, which are respectively the left and right argument of operator itself.</p>
<p>The <code>+</code> operator is present in R base, and can be overloaded as well, like in <code>ggplot2</code> package, where the <code>+</code> operator is used to “build” the graph characteristics, as in following example:</p>
<pre>require(ggplot2)

prds &lt;- predict(object = md, interval = &quot;prediction&quot;, level=.9)
ds &lt;- cbind(ds, prds)
ds$outliers &lt;- as.factor(ds$y&lt;ds$lwr | ds$y>ds$upr)

graph &lt;- ggplot(data = ds,mapping = aes(x=x, y=y, color=outliers))
graph &lt;- graph + geom_point()
graph &lt;- graph + geom_line(aes(y=fit), col=&quot;blue&quot;)
graph &lt;- graph + geom_line(aes(y=lwr), col=&quot;green&quot;)
graph &lt;- graph + geom_line(aes(y=upr), col=&quot;green&quot;)
graph &lt;- graph + ggtitle(&quot;Regression and 90% prediction bands&quot;)
print(graph)</pre>
<p><a target="_blank" rel="attachment wp-att-2365" href="http://i0.wp.com/www.milanor.net/blog/wp-content/uploads/2016/07/Methods_Overloading_fig1-1.png"><img sizes="(max-width: 397px) 100vw, 397px" srcset="http://www.milanor.net/blog/wp-content/uploads/2016/07/Methods_Overloading_fig1-1-150x150.png 150w, http://www.milanor.net/blog/wp-content/uploads/2016/07/Methods_Overloading_fig1-1-300x300.png 300w, http://i0.wp.com/www.milanor.net/blog/wp-content/uploads/2016/07/Methods_Overloading_fig1-1.png?resize=397%2C397 480w" alt="Methods_Overloading_fig1-1" class="wp-image-2365" src="http://i0.wp.com/www.milanor.net/blog/wp-content/uploads/2016/07/Methods_Overloading_fig1-1.png?resize=397%2C397" data-recalc-dims="1"></a></p>
<p> </p>
<p>The <code>+</code> operator, then, is applied differently with <code>ggplot2</code> objects (with respect other object types), where it “concatenates” or “assembles” parts of final graph.</p>
<p>In this small post, and following ones, I would like to produce some “jokes” with objects, operators, overloading, and similar “oddities”..</p>
</div>
<div>
<h2><em>C</em>-language <code>+=</code> operator and its emulation in R</h2>
<p>In <em>C</em> language there are several useful operators that allow the programmer to save some typing and to produce some more efficient and easiest to read code. The first operator that I would like to discuss is the <code>+=</code> one.</p>
<p><code>+=</code> is an operator that does operations like: <code>a = a + k</code>.<br>
In <em>C</em>, the above sentence can be summarized with <code>a += k</code>. Of course, the sentence can be something of more complex, like</p>
<p><code>a += (x-log(2))^2</code></p>
<p>In this case, the code line shall be “translated” to <code>a = a + (x-log(2))^2</code>.</p>
<p>If I would like to have in R a new operator that acts similarly to <em>C</em>’s <code>+=</code>, I whould have to create it.</p>
<p>Unfortunately, not all the names are allowed in R for new operators: if I want to produce a new operator name I can only use operators with names like <code>%mynewoperator%</code> where the <code>%</code> symbols are mandatory.</p>
<p>Indeed, for this example, I will create a new <code>%+=%</code> operator that acts similarly to the <em>C</em>’s <code>+=</code>.</p>
<p>This new operator has to be able to get the values of variables passed as arguments, to sum them, and then, more importantly, to update the value of the first variable with the new value.</p>
</div>
<p><a target="_blank" href="http://www.quantide.com/website/playing-around-methods-overloading-c-language-operators/">Continue reading on Quantide blog</a>…</p>
</div>
<p>The post <a target="_blank" rel="nofollow" href="http://www.milanor.net/blog/playing-around-methods-overloading-c-language-operators/">Playing Around with Methods Overloading, C-language and Operators (1)</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.milanor.net/blog">MilanoR</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/playing-around-with-methods-overloading-c-language-and-operators-1/"></div></div><div><a data-url="http://www.r-bloggers.com/playing-around-with-methods-overloading-c-language-and-operators-1/" data-counturl="http://www.r-bloggers.com/playing-around-with-methods-overloading-c-language-and-operators-1/" data-count="vertical" data-text="Playing Around with Methods Overloading, C-language and Operators (1)" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<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.milanor.net/blog/playing-around-methods-overloading-c-language-operators/"> MilanoR</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/d55BoDmnMeg"></a></div>