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.
  • infix operator postfix operation
  • 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
 r infix operators

Example: How infix operators work in R?

>5+3
[1] 8
>`+`(5,3)
[1] 8

>5-3
[1] 2
>`-`(5,3)
[1] 2

>5*3-1+

[1] 14
>`-`(`*`(5,3),1)
[1] 14
  • 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

`%divisible%`<- function(x,y)
{
if (x%%y ==0) return (TRUE)
elsereturn (FALSE)
}[1] 14
>10 %divisible% 3
[1] FALSE

>10 %divisible% 2
[1] TRUE

>`%divisible%`(10,5)
[1] TRUE
  • 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


Related Searches to R Infix