R Infix - r - learn r - r programming
- The operators that is frequently used in R are binary operators having two operands.
- With infix functions, the function name comes in between its arguments.
- These operators perform a function call in the background.
- For instance, the expression a+b is actually calling the function `+`() with the arguments a and b, as `+`(a, b).
- The back tick (`) is important, because the function name contains special symbols.
- Here, listed some sample expressions along with the actual functions that get called in the background
Example: How infix operators work in R?
- In R, it is possible to create user-defined infix operators.
- This is done by naming a function that starts and ends with %.
- An example of user-defined infix operator to see if a number is exactly divisible by another is given below
Example: User defined infix operator
- This function can be used as infix operator a %divisible% b or as a function call `%divisible%`(a, b). Both are the equivalent.
- It is important to note that infix operators must start and end with %.
- Enclose it with back tick (`) in the function definition and escape any special symbols.
Predefined infix operators in R
%% | Remainder operator |
%/% | Integer division |
%*% | Matrix multiplication |
%o% | Outer product |
%x% | Kronecker product |
%in% | Matching operator |