The ultimate reason is that if you do both general-purpose programming and numerical computations, it is useful to have a large complement of binary operators available. For example, if you store numbers in two-dimensional arrays, you may want to multiply the arrays elementwise, or you may want to compute the matrix product of two arrays. In Matlab these two operators are .*
and *
; in R they are *
and %*%
. Python has resisted attempts to add new operators, and so numpy differentiates between the two kinds of product by having two classes: the array class is multiplied elementwise, the matrix class is multiplied in the linear-algebra sense.
Another example from Python is that for lists, plus means concatenation: [1,2,3]+[4,5] == [1,2,3,4,5]
. But for numpy arrays, plus means elementwise addition: array([1,2]) + array([4,5]) == array([5,7])
. If your code needs to do both, you have to convert between classes or use function notation, which can lead to cumbersome-looking code, especially where mathematics is involved.
So it would sometimes be convenient to have more operators available for use, and you might not know in advance what sorts of operators a particular application calls for. Therefore, the implementors of R have chosen to treat as operators anything named like %foo%
, and several examples exist: %in%
is set membership, %x%
is Kronecker product, %o%
is outer product. For an example of a language that has taken this to the extreme, see Fortress (section 16 of the specification starts with the rules for operator names).
In the blog post you mentioned, the author is using the ggplot2 graphing package, which defines %+%
to mean some kind of combination of two plot elements. Really it seems to add a method to the bare +
(which is a generic function so you can define what it means for user-defined objects), but it also defines %+%
so that you can use the ggplot2 meaning of +
(whatever it is) for other objects. If you install ggplot2, type require(ggplot2)
and ?`%+%`
to see the documentation of that operator, and methods(`+`)
to see that a new definition has been added to +
.