The operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. The R language has rich built-in operators and provides the following types of operators.
Operator type
- Arithmetic operators
- Relational operators
- logical operators
- Assignment operators
- Other operators
Arithmetic operators
The following table lists the arithmetic operators supported with the R language. Each element in the operator's action vector.
| operator |
Description |
Example |
| + |
Add two vectors |
V <-C (2,5.5,6) T <-C (8, 3, 4) print (v+t) It produces the following results:[1] 10.0 8.5 10.0 |
| ? |
Subtract a second vector from the first vector |
V <-C (2,5.5,6) T <-C (8, 3, 4) print (V-T) It produces the following results:[1] -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 results:[1] 16.0 16.5 24.0 |
| / |
Divides the first vector from the second vector |
V <-C (2,5.5,6) T <-C (8, 3, 4) print (v/t) It produces the following results:[1] 0.250000 1.833333 1.500000 |
| %% |
Get the first vector and the second vector remainder |
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 result of dividing the first vector from the second (quotient) |
V <-C (2,5.5,6) T <-C (8, 3, 4) print (v%/%t) It produces the following results:[1] 0 1 1 |
| ^ |
The first vector is promoted to the exponent of the second 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 |
Relational operators
The following table lists the relational operators that are supported in the R language. Each element of the first vector is compared to the corresponding element of the second vector. The result of the comparison is a Boolean value.
| operator |
Description |
Example |
| > |
Checks 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 results:
[1] False TRUE false false |
| < |
Checks whether 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 results:[1] True false True false |
| == |
Checks whether each element of the first vector equals 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:[1] false false TRUE |
| <= |
Checks whether 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:[1] True FALSE true True |
| >= |
Checks 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:[1] False true false |
| != |
Checks whether 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:[1] True True True FALSE |
logical operators
The following table lists the logical operators supported in the R language. It applies only to a logical, numeric, or complex vector. All values greater than 1 are considered true for the logical value.
Each element of the first vector is compared with the corresponding element of the second vector. The result of the comparison is a Boolean value.
| operator |
description |
example |
| & amp; |
This is the so-called element logical AND operator. It combines each element of the first vector with the corresponding element of the second vector, and gives a true if all two components are true. |
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 |
| | |
This is the so-called element-wise logic or operator. It combines each element of the first vector with the corresponding element of the second vector, giving a true if one of these elements is 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 |
| ! |
This is called a logical non-operator. Each element of the orientation amount, and gives the opposite logical value. |
v <-C (3,0,true,2+2i) print (!V) it produces the following result: [1" false TRUE false |
Logical operators && and | | Consider vector only the first element and give a single element as the output vector.
| operator |
Description |
Example |
| && |
The so-called logic and operators. Take the first element of two vectors, only if two are true for the result. |
V <-C (3,0,true,2+2i) T <-C (1,3,true,2+3i) print (v&&t) It produces the following results:[1] TRUE |
| || |
The so-called logic or operator. Takes the first element of two vectors, only if two is true and the result is true. |
V <-C (0,0,true,2+2i) T <-C (0,3,true,2+3i) print (v| | T It produces the following results:[1] FALSE |
Assignment operators
These operators are used to assign values to vectors.
| operator |
Description |
Example |
<- Or = Or <<- |
It's called 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:[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 ->> |
It's called Right assignment. |
C (3,1,true,2+3i), v1c (3,1,true,2+3i)->> v2 print (v1) print (v2) It produces the following results:[1] 3+0i 1+0i 1+0i 2+3i[1] 3+0i 1+0i 1+0i 2+3i |
Other operators
These operators are used for specific purposes, rather than general mathematical or logical operations.
| operator |
Description |
Example |
| : |
The colon operator. It creates a vector of sequential numbers. |
It produces the following results: |
| %in% |
This operator is used to identify whether an element belongs to (in) a vector. |
It produces the following results: |
| %*% |
This operator is used to multiply the transpose matrix of it. |
It produces the following results:
[, 1] [, 2] [1,] 82[2,] 117 |
R language Operators