Intro to Infix Operators in R
Infix operators in R are unique functions and methods that facilitate basic data expressions or transformations.
Infix refers to the placement of the arithmetic operator between variables. For example, an infix operation is given by (a+b), whereas prefix and postfix operators are given by (+ab) and (ab+), respectively.
The types of infix operators used in R include functions for data extraction, arithmetic, sequences, comparison, logical testing, variable assignments, and custom data functions.
The primary infix operators and their rank are listed below. The rank of an operator indicates the order in which a function is processed by R. This is relevant for dealing with complex expressions that involve two or more operators. Rank also confirms if brackets or parenthesis are useful or redundant. Examples in the table use the following data vectors as inputs:
1 2 |
> value1 <- c(7, 4, 4) > value2 <- c(3, 5, 7) |
List of Infix Operators in R
Operator | Rank | Type | Description | Comment |
---|---|---|---|---|
:: | 1 | Extract | Function retrieval | Extract function from a package namespace. my.package::mean extracts a new mean function from my.package |
::: | 1 | Extract | Function retrieval | Extract a hidden function from a namespace |
$ | 2 | Extract | List subset | Extract list data by name. See name() function |
@ | 2 | Extract | Slot selection | Extract attributes by memory slot or location. See slotnames() function |
[ and [[ | 3 | Extract | Subscripting | Extract data by index |
^ | 4 | Arithmetic | Exponential | 2^3 = 8 |
: | 5 | Sequence | Sequence | 1:3 = 1, 2, 3 |
%% | 6 | Arithmetic | Integer Divide | 5 %/% 2 = 2 See floor function |
%% | 6 | Arithmetic | Modulas | 5 %% 2 = 1 |
%*% | 6 | Arithmetic | Matrix Multiplication | Multiplies two matrices that are conformable |
%o% | 6 | Arithmetic | Outer Product | See help("%o%") for examples |
%x% | 6 | Arithmetic | Kronecker product | See help("%x%") for examples |
* | 7 | Arithmetic | Multiplication | Also matrix dot product |
/ | 7 | Arithmetic | Division | n/a |
+ | 8 | Arithmetic | Addition | n/a |
- | 8 | Arithmetic | Subtraction | n/a |
! | 8 | Comparison | Not | n/a |
%in% | 9 | Comparison | Value Matching | value1 %in% value2: true false false |
!= | 9 | Comparison | Not Equal To | value1 != value2: true true true |
< | 9 | Comparison | Less Than | value1< 5: false true true |
> | 9 | Comparison | Greater Than | value2 > 4: false true true |
== | 9 | Comparison | Equal To | value2 == 5: false true false |
<= | 9 | Comparison | Less Than Or Equal To | value1 <= value2: false true true |
>= | 9 | Comparison | Greater Than Or Equal To | value1 >= value2: true false false |
xor | 10 | Logical | Exclusive Or | xor(value1, value2): false, false, false |
& | 10 | Logical | And (element) | value1==4 & value2 >=6: false false true > value1==4&value2 >=6 [1] false false true |
&& | 10 | Logical | And (control) | is.na(value1[2]) && value2[1]==5: false |
| | 10 | Logical | Or (element) | value1==7 | value2>=7: true false true |
|| | 10 | Logical | Or (control) | is.na(value1[1]) || value2[1]==4: false |
~ | 11 | Assignment | Equal | Used in formulas and model building |
<<- | 12 | Assignment | Permanent Assignment | n/a |
<- | 13 | Assignment | Left Assignment | n/a |
-> | 13 | Assignment | Right Assignment | n/a |
= | 13 | Assignment | Argument Assignment | n/a |