Getting Started with R Language - Operator

Operator The

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

1, the type of operator

R has the following operator types:

  • Arithmetic operator
  • Relational operator
  • 逻辑操作符
  • Assignment Operator
  • Other Operators

2, Arithmetic Operators

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

操作符 描述 example
+ Two vectors add
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)

It produces the following result -

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

it produces the following Result -

-6.0  2.5  2.0
* Two vector multiplication
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)

It produces the following result -

16.0 16.5 24.0
/ divides the first vector by the second vector
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 -

0.250000 1.833333 1.500000
%% 两 vector求余
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
%/% Two vectors are divided by the business
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)

It produces the following result -

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

produces the following result -

[1]  256.000  166.375 1296.000

3, relational operator

The following table shows the relational operators supported by the R language. Each element of the first vector is compared to a corresponding element of the second vector. The result of the comparison is a Boolean value.

操作符 描述 example
> Check if each element of the first vector is greater 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 result -

FALSE  TRUE FALSE FALSE
< Checks if each element of the first vector is smaller 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 result -

TRUE FALSE  TRUE FALSE
== Checks if 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 result -

FALSE FALSE FALSE  TRUE
<= Checks if 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 result -

TRUE FALSE  TRUE  TRUE
> = Checks if 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 result -

FALSE  TRUE FALSE  TRUE
!= Checks if 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 result -

TRUE  TRUE  TRUE FALSE

4, logical operator

The following table shows the logical operators supported by the R language. It only works with logic, numbers or complex types of vectors. All numbers greater than 1 are considered to be logical values ​​TRUE. Each element of the first vector is compared to a corresponding element of the second vector. The result of the comparison is a Boolean value.

操作符 描述 example
& It is called the elemental logical AND operator. It combines each element of the first vector with the corresponding element of the second vector, and gives an output TRUE if both elements are TRUE.
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)

It produces the following result -

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 gives an output TRUE if the element is true.
v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)

It produces the following result -

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

It produces the following result -

FALSE  TRUE FALSE FALSE

Logical operators && and || only consider the first element of the vector, giving the vector of the individual element as the output.

操作符 描述 example
&& is called the logical AND operator. TRUE is given when the first element of two vectors is taken and only two are TRUE.
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)

It produces the following result -

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

It produces the following result -

FALSE

5, assignment operator

These operators are used to assign values ​​to vectors.

操作符 描述 example

<−

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 -

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

->>

called right allocation
c(3,1,TRUE,2+3i) -> v1
c(3,1,TRUE,2+3i) ->> v2 
print(v1)
print(v2)

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

6, other operators

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

操作符 描述 example
: 冒号 Operator. It creates a series of numbers in order for the vector.
v <- 2:8
print(v) 

It produces the following result -

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 result -

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