What do the %op% operators in mean? For example "%in%"?

heavy rocker dude picture heavy rocker dude · Oct 4, 2012 · Viewed 45.9k times · Source

I tried to do this simple search but couldn't find anything on the percent (%) symbol in R.

What does %in% mean in the following code?

time(x) %in% time(y) where x and y are matrices.

How do I look up help on %in% and similar functions that follow the %stuff% pattern, as I cannot locate the help file?

Related questions:

Answer

IRTFM picture IRTFM · Oct 4, 2012

I didn't think GSee's or Sathish's answers went far enough because "%" does have meaning all by itself and not just in the context of the %in% operator. It is the mechanism for defining new infix operators by users. It is a much more general issue than the virtues of the %in% infix operator or its more general prefix ancestor match. It could be as simple as making a pairwise "s"(um) operator:

 `%s%` <- function(x,y) x + y

Or it could be more interesting, say making a second derivative operator:

 `%DD%` <- function(expr, nam="x") { D(D( bquote(.(expr)), nam), nam) }
 expression(x^4) %DD% "x"
 # 4 * (3 * x^2)

The %-character also has importance in the parsing of Date, date-time, and C-type format functions like strptime, formatC and sprintf.

Since that was originally written we have seen the emergence of the magrittr package with the dplyr elaboration that demonstrates yet another use for %-flanked operators.

So the most general answer is that % symbols are handled specially by the R parser. Since the parser is used to process plotmath expressions, you will also see extensive options for graphics annotations at the ?plotmath help page.