Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does eg %+% do? in R

Tags:

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 similar used. I guess its a kind of merge?.

EDIT: Ok - I believe my question is answered. %X% is binary operator of some kind. So now I think I will google around for knowledge about how/when to use these. My question was partly inspired by yesterday's question - but only after I saw this post on the "learning R" blog. The passage that gave rise to 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 
like image 703
Andreas Avatar asked Aug 25 '09 15:08

Andreas


People also ask

What does %>% do in Rstudio?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression.

What is the meaning of %% in R?

%% gives Remainder. %/% gives Quotient. So 6 %% 4 = 2. In your example b %% a , it will vectorised over the values in “a”

What is the percent operator in R?

The %in% operator in R can be used to identify if an element (e.g., a number) belongs to a vector or dataframe. For example, it can be used the see if the number 1 is in the sequence of numbers 1 to 10.

How do you do not in R?

The not in r is the Negation of the %in% operator. The %in% operator is used to identify if an element belongs to a vector. The ! indicates logical negation (NOT).


1 Answers

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

like image 81
Jouni K. Seppänen Avatar answered Oct 04 '22 18:10

Jouni K. Seppänen