tags:

views:

265

answers:

3

This is a very basic question - but apparently google is not very good at searching for strings like "%+%". So my question is - what and when is "%+%" and simlar used. I guess its a kind of merge?.

EDIT: Ok - I beleive my question is answered. %X% is binary operator of some kind. So know I think I will google around for knowledge about how/when to use theese. My question was partly inspired by yeasterdays question - but only after I saw this post on the "learning R" blog. The passage that tricked my question was this:
In order to do this, a new dataframe with the annual totals will be created and later merged with the existing dataset (variable names in both dataframes should be identical for this to work). Then we just change the dataframe the plot is based on.

## add total immigration figures to the plot
total <- cast(df.m, Period ~ ., sum)
total <- rename(total, c("(all)" = "value"))
total$Region <- "Total"
df.m.t <- rbind(total, df.m)
c1 <- c %+% df.m.t
+1  A: 

Based on my quick look at the manual it may be a user defined infix operator, so, it's hard to tell what the actual meaning would be...

I would think binary addition.

chills42
Just the plain + is binary addition, I believe - %+% doesn't seem to have anything assigned to it. Try ?Arithmetic for more information.
Matt Parker
+9  A: 

There is no generally defined %+%. Maybe you looked at this question from yesterday where

R> '%+%' <- paste
R> "foo" %+% "bar"
[1] "foo bar"
R>

and ad-hoc string concatenation function was defined. Generally, the 'percent-operator-percent' syntax is open for user-defined functions of two arguments, but there is (AFAIK) no generally accepted version for %+% that you can expect to be present everywhere.

Dirk Eddelbuettel
+6  A: 

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 +.

Jouni K. Seppänen
wauw - thank you so much Jouni! What an excellent answer - it is all beginning to make sense now :-)
Andreas