Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

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

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...