R 简明教程
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.
Types of Operators
We have the following types of operators in R programming −
-
Arithmetic Operators
-
Relational Operators
-
Logical Operators
-
Assignment Operators
-
Miscellaneous Operators
Arithmetic Operators
Following table shows the arithmetic operators supported by R language. The operators act on each element of the vector.
Operator |
Description |
Example |
+ |
Adds two vectors |
Live Demov ← 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 |
− |
Subtracts second vector from the first |
Live Demov ← 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 |
* |
Multiplies both vectors |
Live Demov ← 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 |
/ |
Divide the first vector with the second |
Live Demov ← 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 |
%% |
Give the remainder of the first vector with the second |
Live Demov ← 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 |
%/% |
The result of division of first vector with second (quotient) |
Live Demov ← c( 2,5.5,6) t ← c(8, 3, 4) print(v%/%t) it produces the following result − [1] 0 1 1 |
^ |
The first vector raised to the exponent of second vector |
Live Demov ← 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 |
Relational Operators
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 |
Example |
> |
Checks if each element of the first vector is greater than the corresponding element of the second vector. |
Live Demov ← 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 |
< |
Checks if each element of the first vector is less than the corresponding element of the second vector. |
Live Demov ← 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 |
== |
Checks if each element of the first vector is equal to the corresponding element of the second vector. |
Live Demov ← 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 |
⇐ |
Checks if each element of the first vector is less than or equal to the corresponding element of the second vector. |
Live Demov ← 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 |
>= |
Checks if each element of the first vector is greater than or equal to the corresponding element of the second vector. |
Live Demov ← 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 |
!= |
Checks if each element of the first vector is unequal to the corresponding element of the second vector. |
Live Demov ← 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 |
Logical Operators
下表显示了 R 语言支持的逻辑运算符。它仅适用于逻辑、数值或复杂类型的向量。大于 1 的所有数字都被视为逻辑值 TRUE。
第一个向量的每个元素都与第二个向量的对应元素进行比较。比较的结果是布尔值。
Operator |
Description |
Example |
& |
它称为元素级逻辑 AND 运算符。它将第一个向量的每个元素与第二个向量的对应元素组合在一起,如果两个元素都是 TRUE,则给出一个输出 TRUE。 |
Live Demo v ← c(3,1,TRUE,2+3i)t ← c(4,1,FALSE,2+3i)print(v&t)产生以下结果 −[1] TRUE TRUE FALSE TRUE |
它称为元素级逻辑 OR 运算符。它将第一个向量的每个元素与第二个向量的对应元素组合在一起,如果其中一个元素是 TRUE,则给出一个输出 TRUE。 |
||
Live Demo v ← c(3,0,TRUE,2+2i)t ← c(4,0,FALSE,2+3i)print(v |
t)产生以下结果 −[1] TRUE FALSE TRUE TRUE |
! |
逻辑运算符 && 和 || 仅考虑向量的第一个元素,并给出一个单元素向量的输出。
Operator |
Description |
Example |
&& |
称为逻辑 AND 运算符。获取两个向量的第一个元素,并且仅当两者都为 TRUE 时才给 TRUE。 |
Live Demo v ← c(3,0,TRUE,2+2i)t ← c(1,3,TRUE,2+3i)print(v&&t)产生以下结果 −[1] TRUE |
称为逻辑 OR 运算符。获取两个向量的第一个元素,并且如果其中一个为 TRUE,则给 TRUE。 |
Live Demo v ← c(0,0,TRUE,2+2i)t ← c(0,3,TRUE,2+3i)print(v |
Assignment Operators
这些运算符用于为向量分配值。
Operator |
Description |
Example |
<− or = or <<− |
Called Left Assignment |
Live Demo 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)产生以下结果 −[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 →> |
Called Right Assignment |
Live Demo c(3,1,TRUE,2+3i) → v1c(3,1,TRUE,2+3i) →> v2print(v1)print(v2)产生以下结果 −[1] 3+0i 1+0i 1+0i 2+3i[1] 3+0i 1+0i 1+0i 2+3i |
Miscellaneous Operators
这些运算符用于特殊目的,而不是常规的数学或逻辑运算。
Operator |
Description |
Example |
: |
冒号运算符。它为向量按顺序创建一系列数字。 |
Live Demo v ← 2:8print(v)产生以下结果 −[1] 2 3 4 5 6 7 8 |
%in% |
此运算符用于识别元素是否属于向量。 |
Live Demo v1 ← 8v2 ← 12t ← 1:10print(v1 %in% t)print(v2 %in% t)产生以下结果 −[1] TRUE[1] FALSE |
%*% |
此运算符用于用矩阵与其转置相乘。 |
Live Demo M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)t = M %*% t(M)print(t)产生以下结果 − [,1] [,2][1,] 65 82[2,] 82 117 |