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:
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:
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.
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.
a <- 10
b <- 5
print (a/b)
Output : 2
Power Operator (^)
The first operand is raised to the power of the second operand.
a <- 4
b <- 5
print(a^b)
Output : 1024
Modulo Operator (%%)
The remainder of the first operand divided by the second operand is returned.
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 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.