R Operators – Arithmetic, Relational, Logical, Assignment

R Tutorial – We shall learn about R Operators – Arithmetic, Relational, Logical, Assignment and some of the Miscellaneous Operators that R programming language provides.

R Operators

There are four main categories of Operators in R programming language. They are shown in the following picture :

R Operators
We shall learn about these operators in detail with Example R programs.

 

R Arithmetic Operators

R Arithmetic Operators - R Operators

Arithmetic Operators are used to accomplish arithmetic operations. They can be operated on the basic data types Numericals, Integers, Complex Numbers. Vectors with these basic data types can also participate in arithmetic operations, during which the operation is performed on one to one element basis.

OperatorDescriptionUsage
+Addition of two operandsa + b
Subtraction of second operand from first a – b
*Multiplication of two operandsa * b
/Division of first operand with seconda / b
%%Remainder from division of first operand with seconda %% b
%/%Quotient from division of first operand with seconda %/% b
^First operand raised to the power of second operanda^b

An example for each of the arithmetic operator on Numerical values is provided below :

An example for each of the arithmetic operator on Vectors is provided below :

 

R Relational Operators

R Relational Operators - R Operators

Relational Operators are those that find out relation between the two operands provided to them. Following are the six relational operations R programming language supports.The output is boolean (TRUE or FALSE) for all of the Relational Operators in R programming language.

OperatorDescriptionUsage
<Is first operand less than second operanda < b
>Is first operand greater than second operanda > b
==Is first operand equal to second operanda == b
<=Is first operand less than or equal to second operanda <= b
>=Is first operand greater than or equal to second operanda > = b
!=Is first operand not equal to second operanda!=b

An example for each of the relational operator on Numberical values is provided below :

An example for each of the relational operator on Vectors is provided below :

 

R Logical Operators

R Logical Operators

Logical Operators in R programming language work only for the basic data types logical, numeric and complex and vectors of these basic data types.

OperatorDescriptionUsage
&Element wise logical AND operation.a & b
|Element wise logical OR operation.a | b
!Element wise logical NOT operation.!a
&&Operand wise logical AND operation.a && b
||Operand wise logical OR operation.a || b

An example for each of the logical operators on Numerical values is provided below :

An example for each of the logical operators on Vectors is provided below :

 

R Assignment Operators

R Assignment Operators

Assignment Operators are those that help in assigning a value to the variable.

OperatorDescriptionUsage
=Assigns right side value to left side operanda = 3
<-Assigns right side value to left side operanda <- 5
->Assigns left side value to right side operand4 -> a
<<-Assigns right side value to left side operanda <<- 3.4
->>Assigns left side value to right side operandc(1,2) ->> a

An example for each of the assignment operators is provided below :

 

R Miscellaneous Operators

R Miscellaneous Operators

These operators does not fall into any of the categories mentioned above, but are significantly important during R programming for manipulating data.

OperatorDescriptionUsage
:Creates series of numbers from left operand to right operanda:b
%in%Identifies if an element(a) belongs to a vector(b)a %in% b
%*%Performs multiplication of a vector with its transpose A %*% t(A)

An example for each of the Miscellaneous operators is provided below :

Conclusion :

In this R Tutorial, we have learnt about R Operators – R Arithmetic Operators, R Relational Operators, R Logical Operators, R Assignment Operators, R Miscellaneous Operators with example R commands and R script files.