Arithmetic Operators

Arithmetic Operators modulo using the specified operator between operands, which may be either scalar values, complex numbers, or vectors. The R operators are performed element-wise at the corresponding positions of the vectors. 

Addition operator (+)

The values at the corresponding positions of both operands are added. Consider the following R operator snippet to add two vectors:

R
a <- c (1, 0.1)
b <- c (2.33, 4)
print (a+b)
 Output : 3.33  4.10 

Subtraction Operator (-)

The second operand values are subtracted from the first. Consider the following R operator snippet to subtract two variables:

R
 a <- 6
 b <- 8.4
 print (a-b)
Output : -2.4 

Multiplication Operator (*) 

The multiplication of corresponding elements of vectors and Integers are multiplied with the use of the ‘*’ operator.

R
B= c(4,4) 
C= c(5,5)
print (B*C)
Output : 20 20

Division Operator (/) 

The first operand is divided by the second operand with the use of the ‘/’ operator.

R
 a <- 10
 b <- 5
 print (a/b)
Output : 2

Power Operator (^)

The first operand is raised to the power of the second operand.

R
 a <- 4
 b <- 5
 print(a^b)
Output : 1024

Modulo Operator (%%)

The remainder of the first operand divided by the second operand is returned.

R
list1<- c(2, 22)
list2<-c(2,4)
print(list1 %% list2)
Output : 0  2

The following R code illustrates the usage of all Arithmetic R operators.

R
# R program to illustrate 
# the use of Arithmetic operators
vec1 <- c(0, 2)
vec2 <- c(2, 3)

# Performing operations on Operands
cat ("Addition of vectors :", vec1 + vec2, "\n")
cat ("Subtraction of vectors :", vec1 - vec2, "\n")
cat ("Multiplication of vectors :", vec1 * vec2, "\n")
cat ("Division of vectors :", vec1 / vec2, "\n")
cat ("Modulo of vectors :", vec1 %% vec2, "\n")
cat ("Power operator :", vec1 ^ vec2)

Output 

Addition of vectors : 2 5 
Subtraction of vectors : -2 -1 
Multiplication of vectors : 0 6 
Division of vectors : 0 0.6666667 
Modulo of vectors : 0 2 
Power operator : 0 8

R Operators

Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. 

Similar Reads

R Operators

R supports majorly four kinds of binary operators between a set of operands. In this article, we will see various types of operators in R Programming language and their usage....

Arithmetic Operators

Arithmetic Operators modulo using the specified operator between operands, which may be either scalar values, complex numbers, or vectors. The R operators are performed element-wise at the corresponding positions of the vectors....

Logical Operators

Logical Operators in R simulate element-wise decision operations, based on the specified operator between the operands, which are then evaluated to either a True or False boolean value. Any non-zero integer value is considered as a TRUE value, be it a complex or real number....

Relational Operators

The Relational Operators in R carry out comparison operations between the corresponding elements of the operands. Returns a boolean TRUE value if the first operand satisfies the relation compared to the second. A TRUE value is always considered to be greater than the FALSE....

Assignment Operators

Assignment Operators in R are used to assigning values to various data objects in R. The objects may be integers, vectors, or functions. These values are then stored by the assigned variable names. There are two kinds of assignment operators: Left and Right...

Miscellaneous Operators

Miscellaneous Operator are the mixed operators in R that simulate the printing of sequences and assignment of vectors, either left or right-handed....