Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>.*</code> and <code>*</code>; in R they are <code>*</code> and <code>%*%</code>. Python has <a href="http://www.python.org/dev/peps/pep-0211/" rel="noreferrer">resisted</a> <a href="http://www.python.org/dev/peps/pep-0225/" rel="noreferrer">attempts</a> to add new operators, and so <a href="http://numpy.scipy.org/" rel="noreferrer">numpy</a> 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.</p> <p>Another example from Python is that for lists, plus means concatenation: <code>[1,2,3]+[4,5] == [1,2,3,4,5]</code>. But for numpy arrays, plus means elementwise addition: <code>array([1,2]) + array([4,5]) == array([5,7])</code>. 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.</p> <p>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 <code>%foo%</code>, and several examples exist: <code>%in%</code> is set membership, <code>%x%</code> is Kronecker product, <code>%o%</code> is outer product. For an example of a language that has taken this to the extreme, see <a href="https://web.archive.org/web/20110716162714/http://projectfortress.sun.com/" rel="noreferrer">Fortress</a> (section 16 of the specification starts with the rules for operator names).</p> <p>In the blog post you mentioned, the author is using the <a href="http://had.co.nz/ggplot2/" rel="noreferrer">ggplot2</a> graphing package, which defines <code>%+%</code> to mean some kind of combination of two plot elements. Really it seems to add a method to the bare <code>+</code> (which is a generic function so you can define what it means for user-defined objects), but it also defines <code>%+%</code> so that you can use the ggplot2 meaning of <code>+</code> (whatever it is) for other objects. If you install ggplot2, type <code>require(ggplot2)</code> and <code>?&#x60;%+%&#x60;</code> to see the documentation of that operator, and <code>methods(&#x60;+&#x60;)</code> to see that a new definition has been added to <code>+</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload