I know the basics like ==
and !=
, or even the difference (vaguely) between &
and &&
. But stuff like %in%
and %%
and some stuff used in the context of sprintf()
, like sprintf("%.2f", x)
stuff I have no idea about.
Worst of all, they're hard to search for on the Internet because they're special characters and I don't know what they're called...
The R Language Definition, section 3.1.4 refers to them as "special binary operators". One of the ways they're special is that users can define new binary operators using the
%x%
syntax (wherex
is any valid name).The Writing your own functions section of An Introduction to R, refers to them as Binary Operators (which is somewhat confusing because
+
is also a binary operator):They don’t have a special name as far as I know. They are described in R operator syntax and precedence.
The
%anything%
operators are just normal functions, which can be defined by yourself. You do need to put the name of the operator in backticks (`…`
), though: this is how R treats special names.The
sprintf
format strings are entirely unrelated, they are not operators at all. Instead, they are just the conventional C-style format strings.There are several different things going on here with the percent symbol:
Binary Operators
As several have already pointed out, things of the form
%%
,%in%
,%*%
are binary operators (respectively modulo, match, and matrix multiply), just like a+
,-
, etc. They are functions that operate on two arguments that R recognizes as being special due to their name structure (starts and ends with a%
). This allows you to use them in form:instead of the more traditional:
Keep in mind that the following are equivalent:
R just recognizes the standard operators as well as the
%x%
operators as special and allows you to use them as traditional binary operators if you don't quote them. If you quote them (in the examples above with backticks), you can use them as standard two argument functions.Custom Binary Operators
The big difference between the standard binary operators and
%x%
operators is that you can define custom binary operators and R will recognize them as special and treat them as binary operators:Here we defined a binary operator version of the sample function
"%" (Percent) as a token in special function
The meaning of
"%"
in function likesprintf
orformat
is completely different and has nothing to do with binary operators. The key thing to note is that in those functions the%
character is part of a quoted string, and not a standard symbol on the command line (i.e."%"
and%
are very different). In the context ofsprintf
, inside a string,"%"
is a special character used to recognize that the subsequent characters have a special meaning and should not be interpreted as regular text. For example, in:"%.2f"
means a floating point number (f
) to be displayed with two decimals (.2
). Notice how the"I'm a number: "
piece is interpreted literally. The use of"%"
allowssprintf
users to mix literal text with special instructions on how to represent the othersprintf
arguments.The help file, and the general entry, is indeed a good starting point:
?'%in%'
For example, you can see how the operator
'%in%'
is defined:"%in%" <- function(x, table) match(x, table, nomatch = 0) > 0
You can even create your own operators:
'%ni%' <- Negate('%in%')