Data Infix Operators in R

Intro to Infix Operators in R

postfixInfix 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:

List of Infix Operators in R

::1ExtractFunction retrievalExtract function from a package namespace. my.package::mean extracts a new mean function from my.package
:::1ExtractFunction retrievalExtract a hidden function from a namespace
$2ExtractList subsetExtract list data by name. See name() function
@2ExtractSlot selectionExtract attributes by memory slot or location. See slotnames() function
[ and [[3ExtractSubscriptingExtract data by index
^4ArithmeticExponential2^3 = 8
:5SequenceSequence 1:3 = 1, 2, 3
%%6ArithmeticInteger Divide5 %/% 2 = 2 See floor function
%%6ArithmeticModulas5 %% 2 = 1
%*%6ArithmeticMatrix MultiplicationMultiplies two matrices that are conformable
%o%6ArithmeticOuter ProductSee help("%o%") for examples
%x%6ArithmeticKronecker productSee help("%x%") for examples
*7ArithmeticMultiplicationAlso matrix dot product
%in%9ComparisonValue Matchingvalue1 %in% value2: true false false
!=9ComparisonNot Equal Tovalue1 != value2: true true true
<9ComparisonLess Thanvalue1< 5: false true true
>9ComparisonGreater Thanvalue2 > 4: false true true
==9ComparisonEqual Tovalue2 == 5: false true false
<=9ComparisonLess Than Or Equal Tovalue1 <= value2: false true true
>=9ComparisonGreater Than Or Equal Tovalue1 >= value2: true false false
xor10LogicalExclusive Orxor(value1, value2): false, false, false
&10LogicalAnd (element)value1==4 & value2 >=6: false false true
> value1==4&value2 >=6 [1] false false true
&&10LogicalAnd (control)[2]) && value2[1]==5: false
|10LogicalOr (element)value1==7 | value2>=7: true false true
||10LogicalOr (control)[1]) || value2[1]==4: false
~11AssignmentEqualUsed in formulas and model building
<<-12AssignmentPermanent Assignmentn/a
<-13AssignmentLeft Assignmentn/a
->13AssignmentRight Assignmentn/a
=13AssignmentArgument Assignmentn/a

Back | Next

Leave a Reply