R - Operators

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 −
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Following table shows the arithmetic operators supported by R language. The operators act on each element of the vector.
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)
it produces the following result −
[1] 10.0  8.5  10.0

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)
it produces the following result −
[1] -6.0  2.5  2.0

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)
it produces the following result −
[1] 16.0 16.5 24.0

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)
When we execute the above code, it produces the following result −
[1] 0.250000 1.833333 1.500000

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)
it produces the following result −
[1] 2.0 2.5 2.0

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)
it produces the following result −
[1] 0 1 1

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)
it produces the following result −
[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.
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)
it produces the following result −
[1] FALSE  TRUE FALSE FALSE

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v < t)
it produces the following result −
[1]  TRUE FALSE  TRUE FALSE

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v == t)
it produces the following result −
[1] FALSE FALSE FALSE  TRUE

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v<=t)
it produces the following result −
[1]  TRUE FALSE  TRUE  TRUE

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>=t)
it produces the following result −
[1] FALSE  TRUE FALSE  TRUE

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v!=t)
it produces the following result −
[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.
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
it produces the following result −
[1]  TRUE  TRUE FALSE  TRUE

v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)
it produces the following result −
[1]  TRUE FALSE  TRUE  TRUE

v <- c(3,0,TRUE,2+2i)
print(!v)
it produces the following result −
[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.
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)
it produces the following result −
[1] TRUE

v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)
it produces the following result −
[1] FALSE

These operators are used to assign values to vectors.
<−
or
=
or
<<−
v1 <- c(3,1,TRUE,2+3i)
v2 <<- c(3,1,TRUE,2+3i)
v3 = c(3,1,TRUE,2+3i)
print(v1)
print(v2)
print(v3)
it produces the following result −
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i

->
or
->>
c(3,1,TRUE,2+3i) -> v1
c(3,1,TRUE,2+3i) ->> v2 
print(v1)
print(v2)
it produces the following result −
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i

These operators are used to for specific purpose and not general mathematical or logical computation.
v <- 2:8
print(v) 
it produces the following result −
[1] 2 3 4 5 6 7 8

v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t) 
print(v2 %in% t) 
it produces the following result −
[1] TRUE
[1] FALSE

M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
t = M %*% t(M)
print(t)
it produces the following result −
      [,1] [,2]
[1,]   65   82
[2,]   82  117


    12 Lectures 
     2 hours 

 Nishant Malik

    10 Lectures 
     1.5 hours 

 Nishant Malik

    12 Lectures 
     2.5 hours 

 Nishant Malik

    20 Lectures 
     2 hours 

 Asif Hussain

    10 Lectures 
     1.5 hours 

 Nishant Malik

    48 Lectures 
     6.5 hours 

 Asif Hussain
 Print
 Add Notes
 Bookmark this page