Introduction

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.

For example, the summary(x) method performs different operations depending on the so-called “class” of x:

1

2

3

x<-data.frame(first=1:10,second=letters[1:10])

class(x)# An object of class "data.frame"

1

2

## [1] "data.frame"

1

2

summary(x)

1

2

3

4

5

6

7

8

9

## 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

1

2

3

4

ds<-data.frame(x=1:100,y=100+3*1:100+rnorm(100,sd=10))

md<-lm(formula=y~x,data=ds)

class(md)# An object of class "lm"

1

2

## [1] "lm"

1

2

summary(md)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

##

## 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 <2e-16 ***

## x 2.98890 0.03431 87.12 <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: < 2.2e-16

the outputs reported, and the calculations performed, by the summary() method are really different for the x and the mdobjects.

This behavior is one of characteristics of OO languages, and it is called methods overloading.

The methods overloading can be applied not only for methods in “function form” (i.e., for methods like summary()), but also for operators; indeed, “behind the scenes”, the operators are functions/methods. For example, if we try to write + in the R console we obtain:

1

2

`+`

1

2

## function (e1, e2) .Primitive("+")

That means that the + operator actually is a function/method that requires two arguments: e1 and e2, which are respectively the left and right argument of operator itself.

The + operator is present in R base, and can be overloaded as well, like in ggplot2 package, where the + operator is used to “build” the graph characteristics, as in following example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

require(ggplot2)

prds<-predict(object=md,interval="prediction",level=.9)

ds<-cbind(ds,prds)

ds$outliers<-as.factor(ds$y<ds$lwr|ds$y>ds$upr)

graph<-ggplot(data=ds,mapping=aes(x=x,y=y,color=outliers))

graph<-graph+geom_point()

graph<-graph+geom_line(aes(y=fit),col="blue")

graph<-graph+geom_line(aes(y=lwr),col="green")

graph<-graph+geom_line(aes(y=upr),col="green")

graph<-graph+ggtitle("Regression and 90% prediction bands")

print(graph)

The + operator, then, is applied differently with ggplot2 objects (with respect other object types), where it “concatenates” or “assembles” parts of final graph.

In this small post, and following ones, I would like to produce some “jokes” with objects, operators, overloading, and similar “oddities”..

C-language += operator and its emulation in R

In C 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 += one.

+= is an operator that does operations like: a = a + k.
In C, the above sentence can be summarized with a += k. Of course, the sentence can be something of more complex, like

a += (x-log(2))^2

In this case, the code line shall be “translated” to a = a + (x-log(2))^2.

If I would like to have in R a new operator that acts similarly to C’s +=, I whould have to create it.

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 %mynewoperator% where the % symbols are mandatory.

Indeed, for this example, I will create a new %+=% operator that acts similarly to the C’s +=.

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.