Creating infix operators
In the previous recipe, we learned how to create a user-defined function. Most of the functions that we mentioned so far are prefix
functions, where the arguments are in between the parenthesis after the function name. However, this type of syntax makes a simple binary operation of two variables harder to read as we are more familiar with placing an operator in between two variables. To solve the concern, we will show you how to create an infix operator in this recipe.
Getting ready
Ensure that you completed the previous recipes by installing R on your operating system.
How to do it...
Perform the following steps to create an infix operator in R:
- First, let's take a look at how to transform infix operation to prefix operation:
> 3 + 5 [1] 8 > '+'(3,5) [1] 8
- Furthermore, we can look at a more advanced example of the transformation:
> 3:5 * 2 - 1 [1] 5 7 9 > '-'('*'(3:5, 2), 1) [1] 5 7 9
- Moving on, we can create our infix function that finds the intersection between two vectors:
>x <-c(1,2,3,3, 2) >y <-c(2,5) > '%match%' <- function(a,b){ + intersect(a, b) + } >x %match% y [1] 3
- Let's also create a
%diff%
infix to extract the set difference between two vectors:> '%diff%' <- function(a,b){ + setdiff(a, b) + } >x %diff% y [1] 1 2
- Lastly, we can use the infix operator to extract the intersection of three vectors. Or, we can use the
Reduce
function to apply the operation to the list:>x %match% y %match% z [1] 3 > s <- list(x,y,z) >Reduce('%match%',s) [1] 3
How it works...
In a standard function, if we want to perform some operations on the a
and b
variables, we would probably create a function in the form of func(a,b)
. While this form is the standard function syntax, it is harder to read than regular mathematical notation, (that is, a * b
). However, we can create an infix operator to simplify the function syntax.
Before creating our infix operator, we examine different syntax when we apply a binary operator on two variables. In the first step, we demonstrate how to perform arithmetic operations with binary operators. Similar to a standard mathematical formula, all we need to do is to place a binary operator between two variables. On the other hand, we can transform the representation from infix form to prefix form. Like a standard function, we can use the binary operator as the function name, and then we can place the variables in between the parentheses.
In addition to using a predefined infix operator in R, the user can define the infix operator. To create an operator, we need to name the function that starts and ends with %
, and surround the name with a single quote ('
) or back tick (`
). Here, we create an infix operator named %match%
to extract the interaction between two vectors. We can also create another infix function named %diff%
to extract the set difference between two vectors. Lastly, though we can apply the created infix function to more than two vectors, we can use the reduce function to apply the %match%
operation on the list.
There's more...
We can also overwrite the existing operator by creating an infix operator with the same name:
>'+' <- function(x,y) paste(x,y, sep = "|") > x = '123' > y = '456' >x+y [1] "123|456"
Here, we can concatenate two strings with the +
operator.