Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

R language operator


May 12, 2021 R language tutorial


Table of contents


An operator is a symbol that tells the compiler to perform specific mathematical or logical operations. T he R language has a wealth of built-in operators and provides the following types of operators.

The type of operator

There are several operator types in the R language:

  • Arithmetic operator
  • The relationship operator
  • The logical operator
  • The assignment operator
  • Other operators

Arithmetic operator

The following table shows the arithmetic operators supported by the R language. T he operator works on each element of the vector.

Operator Describe Cases
+ The two vectors add up
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)

It produces the following results -

10.0  8.5  10.0
- The two vectors are subtracted
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)

It produces the following results -

-6.0  2.5  2.0
* The two vectors multiply each other
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)

It produces the following results -

16.0 16.5 24.0
/ Divide the first vector from the second
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)

When we execute the code above, it produces the following results -

0.250000 1.833333 1.500000
%% Two vectors for balance
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)

It produces the following results -

[1] 2.0 2.5 2.0
%/% The two vectors divide each other for business
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)

It produces the following results -

[1] 0 1 1
^ The index of the second vector as the first vector
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)

It produces the following results -

[1]  256.000  166.375 1296.000

The relationship operator

The following table shows the relationship operators supported by the R language. C ompare each element of the first vector with the corresponding element of the second vector. T he result of the comparison is the Boolean value.

Operator Describe Cases
> Check that each element of the first vector is larger than the corresponding element of the second vector.
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)

It produces the following results -

FALSE  TRUE FALSE FALSE
< Check that each element of the first vector is less than the corresponding element of the second vector.
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v < t)

It produces the following results -

TRUE FALSE  TRUE FALSE
== Check that each element of the first vector is equal 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)

It produces the following results -

FALSE FALSE FALSE  TRUE
<= Check that each element of the first vector is less than or equal 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)

It produces the following results -

TRUE FALSE  TRUE  TRUE
> = Check whether each element of the first vector is greater than or equal 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)

It produces the following results -

FALSE  TRUE FALSE  TRUE
!= Check that each element of the first vector is not equal 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)

It produces the following results -

TRUE  TRUE  TRUE FALSE

The logical operator

The following table shows the logical operators supported by the R language. I t applies only to vectors of logic, numbers, or complex types. All numbers greater than 1 are considered logical value TRUE.
Compare each element of the first vector with the corresponding element of the second vector. The result of the comparison is the Boolean value.

Operator Describe Cases
& It is called the element logic AND operator. It combines each element of the first vector with the corresponding element of the second vector, and if both elements are TRUE, the output TRUE is given.
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)

It produces the following results -

TRUE  TRUE FALSE  TRUE
| It is called element logic or operator. It combines each element of the first vector with the corresponding element of the second vector, and if the element is true, the output TRUE is given.
v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)

It produces the following results -

TRUE FALSE  TRUE  TRUE
! It is called a logical non-operator. Each element of the vector is obtained and the opposite logical value is given.
v <- c(3,0,TRUE,2+2i)
print(!v)

It produces the following results -

FALSE  TRUE FALSE FALSE

The logical operators, and the || C onsider only the first element of the vector, giving the vector of a single element as the output.

Operator Describe Cases
&& It is called a logical AND operator. Take the first element of both vectors, and only true is given when both are TRUE.
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)

It produces the following results -

TRUE
|| It is called a logical OR operator. Take the first element of the two vectors, and if one of them is TRUE, give TRUE.
v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)

It produces the following results -

FALSE

The assignment operator

These operators are used to assign values to vectors.

Operator Describe Cases

<−

or

=

or

<<−

It is called a 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)
print(v2)
print(v3)

It produces the following results -

3+0i 1+0i 1+0i 2+3i
3+0i 1+0i 1+0i 2+3i
3+0i 1+0i 1+0i 2+3i

->

or

->>

It is called a right assignment
c(3,1,TRUE,2+3i) -> v1
c(3,1,TRUE,2+3i) ->> v2 
print(v1)
print(v2)

It produces the following results -

3+0i 1+0i 1+0i 2+3i
3+0i 1+0i 1+0i 2+3i

Other operators

These operators are used for specific purposes, not for general mathematical or logical calculations.

Operator Describe Cases
: The colon operator. It creates a series of numbers for the vectors in order.
v <- 2:8
print(v) 

It produces the following results -

2 3 4 5 6 7 8
%in% This operator is used to identify whether an element belongs to a vector.
v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t) 
print(v2 %in% t) 

It produces the following results -

TRUE
FALSE
%*% This operator is used to multiply the matrix by its transplication.
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 results -

      [,1] [,2]
[1,]   65   82
[2,]   82  117