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.
- Adds two vectors (+)
c( 2,5.5,6)
v <- c(8, 3, 4)
t <-print(v+t)
## [1] 10.0 8.5 10.0
- Subtracts second vector from the first (-)
c( 2,5.5,6)
v <- c(8, 3, 4)
t <-print(v-t)
## [1] -6.0 2.5 2.0
- Multiplies both vectors (*)
c( 2,5.5,6)
v <- c(8, 3, 4)
t <-print(v*t)
## [1] 16.0 16.5 24.0
- Divide the first vector with the second (/)
c( 2,5.5,6)
v <- c(8, 3, 4)
t <-print(v/t)
## [1] 0.250000 1.833333 1.500000
- Give the remainder of the first vector with the second (%%)
c( 2,5.5,6)
v <- c(8, 3, 4)
t <-print(v%%t)
## [1] 2.0 2.5 2.0
- The result of division of first vector with second (quotient - %/%)
c( 2,5.5,6)
v <- c(8, 3, 4)
t <-print(v%/%t)
## [1] 0 1 1
- The first vector raised to the exponent of second vector (^)
c( 2,5.5,6)
v <- c(8, 3, 4)
t <-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.
c(2,5.5,6,9)
v <- c(8,2.5,14,9)
t <-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.
- Logical And (&)
c(3,1,TRUE,2+3i)
v <- c(4,1,FALSE,2+3i)
t <-print(v&t)
## [1] TRUE TRUE FALSE TRUE
- Logical Or (|)
c(3,0,TRUE,2+2i)
v <- c(4,0,FALSE,2+3i)
t <-print(v|t)
## [1] TRUE FALSE TRUE TRUE
- Logical NOT (!)
c(3,0,TRUE,2+2i)
v <-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.
- Logical AND (&&)
c(3,0,TRUE,2+2i)
v <- c(1,3,TRUE,2+3i)
t <-print(v&&t)
## [1] TRUE
- Logical OR (||)
c(0,0,TRUE,2+2i)
v <- c(0,3,TRUE,2+3i)
t <-print(v||t)
## [1] FALSE
4.1.4 Assignment Operators
These operators are used to assign values to vectors.
- Left Assignment (<-, <<-, =)
c(3,1,TRUE,2+3i)
v1 <- c(3,1,TRUE,2+3i)
v2 <<- c(3,1,TRUE,2+3i)
v3 =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
- 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.
- Colon operator, it creates the series of numbers in sequence for a vector (:)
2:8
v <-print(v)
## [1] 2 3 4 5 6 7 8
- This operator is used to identify if an element belongs to a vector. (%in%)
8
v1 <- 12
v2 <- 1:10
t <-print(v1 %in% t)
## [1] TRUE
print(v2 %in% t)
## [1] FALSE
- This operator is used to multiply a matrix with its transpose (%*%)
matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
M = M %*% t(M)
out =print(out)
## [,1] [,2]
## [1,] 65 82
## [2,] 82 117