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.
We have the following types of operators in R programming −
Following table shows the arithmetic operators supported by R language. The operators act on each element of the vector.
| Operator | Description |
|---|---|
| + | Adds two vectors |
| - | Subtracts second vector from the first |
| * | Multiplies both vectors |
| / | Divide the first vector with the second |
| %% | Give the remainder of the first vector with the second |
| %/% | The result of division of first vector with second (quotient) |
| ^ | The first vector raised to the exponent of second vector |
# Addition
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)
## [1] 10.0 8.5 10.0
# Subtract
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)
## [1] -6.0 2.5 2.0
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)
## [1] 16.0 16.5 24.0
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)
## [1] 0.250000 1.833333 1.500000
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)
## [1] 2.0 2.5 2.0
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)
## [1] 0 1 1
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)
## [1] 256.000 166.375 1296.000
Following table shows the relational operators supported by R language. Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.
| Operator | Description |
|---|---|
| > | Checks if each element of the first vector is greater than the corresponding element of the second vector. |
| < | Checks if each element of the first vector is less than the corresponding element of the second vector. |
| == | Checks if each element of the first vector is equal to the corresponding element of the second vector. |
| <= | Checks if each element of the first vector is less than or equal to the corresponding element of the second vector. |
| >= | Checks if each element of the first vector is greater than or equal to the corresponding element of the second vector. |
| != | Checks if each element of the first vector is unequal to the corresponding element of the second vector. |
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)
## [1] FALSE TRUE FALSE FALSE
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v < t)
## [1] TRUE FALSE TRUE FALSE
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v == t)
## [1] FALSE FALSE FALSE TRUE
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v<=t)
## [1] TRUE FALSE TRUE TRUE
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>=t)
## [1] FALSE TRUE FALSE TRUE
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v!=t)
## [1] TRUE TRUE TRUE FALSE
Following table shows the logical operators supported by R language. 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.
– The logical operator && and || considers only the first element of the vectors and give a vector of single element as output.
| Operator | Description |
|---|---|
| & | It is called Element-wise Logical AND operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if both the elements are TRUE. |
| | | It is called Element-wise Logical OR operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if one the elements is TRUE. |
| ! | It is called Logical NOT operator. Takes each element of the vector and gives the opposite logical value. |
| && | Called Logical AND operator. Takes first element of both the vectors and gives the TRUE only if both are TRUE. |
| || | Called Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE. |
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
## [1] TRUE TRUE FALSE TRUE
v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)
## [1] TRUE FALSE TRUE TRUE
v <- c(3,0,TRUE,2+2i)
print(!v)
## [1] FALSE TRUE FALSE FALSE
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)
## [1] TRUE
v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)
## [1] FALSE