Chapter 4 Operator and Loops

4.1 Operator

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides following types of operators.

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Miscellaneous Operators

4.1.1 Arithmetic Operators

The following operators act on each element of the vector.

  1. Adds two vectors (+)
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)
## [1] 10.0  8.5 10.0
  1. Subtracts second vector from the first (-)
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)
## [1] -6.0  2.5  2.0
  1. Multiplies both vectors (*)
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)
## [1] 16.0 16.5 24.0
  1. Divide the first vector with the second (/)
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)
## [1] 0.250000 1.833333 1.500000
  1. Give the remainder of the first vector with the second (%%)
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)
## [1] 2.0 2.5 2.0
  1. The result of division of first vector with second (quotient - %/%)
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)
## [1] 0 1 1
  1. The first vector raised to the exponent of second vector (^)
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)
## [1]  256.000  166.375 1296.000

4.1.2 Relational Operators

R language support: <, <=, >, >=, ==, !=. Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v > t)
## [1] FALSE  TRUE FALSE FALSE
print(v < t)
## [1]  TRUE FALSE  TRUE FALSE
print(v == t)
## [1] FALSE FALSE FALSE  TRUE
print(v <= t)
## [1]  TRUE FALSE  TRUE  TRUE
print(v >= t)
## [1] FALSE  TRUE FALSE  TRUE
print(v != t)
## [1]  TRUE  TRUE  TRUE FALSE

4.1.3 Logical Operators

It is applicable only to vectors of type logical, numeric or complex. All numbers greater than 1 are considered as logical value TRUE.

Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.

  1. Logical And (&)
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
## [1]  TRUE  TRUE FALSE  TRUE
  1. Logical Or (|)
v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)
## [1]  TRUE FALSE  TRUE  TRUE
  1. Logical NOT (!)
v <- c(3,0,TRUE,2+2i)
print(!v)
## [1] FALSE  TRUE FALSE FALSE

The logical operator && and || considers only the first element of the vectors and give a vector of single element as output.

  1. Logical AND (&&)
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)
## [1] TRUE
  1. Logical OR (||)
v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)
## [1] FALSE

4.1.4 Assignment Operators

These operators are used to assign values to vectors.

  1. Left Assignment (<-, <<-, =)
v1 <- c(3,1,TRUE,2+3i)
v2 <<- c(3,1,TRUE,2+3i)
v3 = c(3,1,TRUE,2+3i)
print(v1)
## [1] 3+0i 1+0i 1+0i 2+3i
print(v2)
## [1] 3+0i 1+0i 1+0i 2+3i
print(v3)
## [1] 3+0i 1+0i 1+0i 2+3i
  1. Right Assignment (->, ->>)
c(3,1,TRUE,2+3i) -> v1
c(3,1,TRUE,2+3i) ->> v2 
print(v1)
## [1] 3+0i 1+0i 1+0i 2+3i
print(v2)
## [1] 3+0i 1+0i 1+0i 2+3i

4.1.5 Miscellaneous Operators

These operators are used to for specific purpose and not general mathematical or logical computation.

  1. Colon operator, it creates the series of numbers in sequence for a vector (:)
v <- 2:8
print(v) 
## [1] 2 3 4 5 6 7 8
  1. This operator is used to identify if an element belongs to a vector. (%in%)
v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t) 
## [1] TRUE
print(v2 %in% t) 
## [1] FALSE
  1. This operator is used to multiply a matrix with its transpose (%*%)
M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
out = M %*% t(M)
print(out)
##      [,1] [,2]
## [1,]   65   82
## [2,]   82  117