📅  最后修改于: 2021-01-08 09:28:41             🧑  作者: Mango
在计算机编程中,运算符是代表动作的符号。运算符是一个符号,告诉编译器执行特定的逻辑或数学操作。 R编程在内置运算符非常丰富。
在R编程中,有不同类型的运算符,每个运算符执行不同的任务。对于数据处理,还有一些高级运算符,例如模型公式和列表索引。
R中使用以下类型的运算符:
算术运算运算符是用于表示算术数学运算的符号。运算符作用于向量的每个元素。 R支持各种算术运算运算符。
S. No | Operator | Description | Example |
---|---|---|---|
1. | + | This operator is used to add two vectors in R.a <- c(2, 3.3, 4) |
b <- c(11, 5, 3) print(a+b) It will give us the following output: [1] 13.0 8.3 5.0 |
2. | – | This operator is used to divide a vector from another one.a <- c(2, 3.3, 4) |
b <- c(11, 5, 3) print(a-b) It will give us the following output: [1] -9.0 -1.7 3.0 |
3. | * | This operator is used to multiply two vectors with each other.a <- c(2, 3.3, 4) |
b <- c(11, 5, 3) print(a*b) It will give us the following output: [1] 22.0 16.5 4.0 |
4. | / | This operator divides the vector from another one.a <- c(2, 3.3, 4) |
b <- c(11, 5, 3) print(a/b) It will give us the following output: [1] 0.1818182 0.6600000 4.0000000 |
5. | %% | This operator is used to find the remainder of the first vector with the second vector.a <- c(2, 3.3, 4) |
b <- c(11, 5, 3) print(a%%b) It will give us the following output: [1] 2.0 3.3 0 |
6. | %/% | This operator is used to find the division of the first vector with the second(quotient). |
a <- c(2, 3.3, 4) b <- c(11, 5, 3) print(a%/%b) It will give us the following output: [1] 0 0 4 |
7. | ^ | This operator raised the first vector to the exponent of the second vector.a <- c(2, 3.3, 4) |
b <- c(11, 5, 3) print(a^b) It will give us the following output: [1] 0248.0000 391.3539 4.0000 |
关系运算符是定义两个实体之间某种关系的符号。这些包括数值等式和不等式。关系运算符将第一向量的每个元素与第二向量的对应元素进行比较。比较的结果将是一个布尔值。 R支持以下关系运算符:
S. No | Operator | Description | Example |
---|---|---|---|
1. | > | This operator will return TRUE when every element in the first vector is greater than the corresponding element of the second vector. |
a <- c(1, 3, 5) b <- c(2, 4, 6) print(a>b) It will give us the following output: [1] FALSE FALSE FALSE |
2. | < | This operator will return TRUE when every element in the first vector is less then the corresponding element of the second vector. |
a <- c(1, 9, 5) b <- c(2, 4, 6) print(a |
3. | <= | This operator will return TRUE when every element in the first vector is less than or equal to the corresponding element of another vector. |
a <- c(1, 3, 5) b <- c(2, 3, 6) print(a<=b) It will give us the following output: [1] TRUE TRUE TRUE |
4. | >= | This operator will return TRUE when every element in the first vector is greater than or equal to the corresponding element of another vector. |
a <- c(1, 3, 5) b <- c(2, 3, 6) print(a>=b) It will give us the following output: [1] FALSE TRUE FALSE |
5. | == | This operator will return TRUE when every element in the first vector is equal to the corresponding element of the second vector. |
a <- c(1, 3, 5) b <- c(2, 3, 6) print(a==b) It will give us the following output: [1] FALSE TRUE FALSE |
6. | != | This operator will return TRUE when every element in the first vector is not equal to the corresponding element of the second vector. |
a <- c(1, 3, 5) b <- c(2, 3, 6) print(a>=b) It will give us the following output: [1] TRUE FALSE TRUE |
逻辑运算符允许程序根据多种条件做出决定。在程序中,每个操作数都被视为可以评估为假值或真值的条件。条件的值用于确定op1运算符op2的总值。逻辑运算符适用于类型为逻辑,数字或复数的向量。
逻辑运算符将第一向量的每个元素与第二向量的对应元素进行比较。
S. No | Operator | Description | Example |
---|---|---|---|
1. | & | This operator is known as the Logical AND operator. This operator takes the first element of both the vector and returns TRUE if both the elements are TRUE. |
a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE, 2+3i) print(a&b) It will give us the following output: [1] TRUE FALSE TRUE TRUE |
2. | | | This operator is called the Logical OR operator. This operator takes the first element of both the vector and returns TRUE if one of them is TRUE. |
a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE, 2+3i) print(a|b) It will give us the following output: [1] TRUE TRUE TRUE TRUE |
3. | ! | This operator is known as Logical NOT operator. This operator takes the first element of the vector and gives the opposite logical value as a result. |
a <- c(3, 0, TRUE, 2+2i) print(!a) It will give us the following output: [1] FALSE TRUE FALSE FALSE |
4. | && | This operator takes the first element of both the vector and gives TRUE as a result, only if both are TRUE. |
a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE, 2+3i) print(a&&b) It will give us the following output: [1] TRUE |
5. | || | This operator takes the first element of both the vector and gives the result TRUE, if one of them is true. |
a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE, 2+3i) print(a||b) It will give us the following output: [1] TRUE |
赋值运算符用于将新值赋给变量。在R中,这些运算符用于为向量分配值。有以下几种类型的作业
S. No | Operator | Description | Example |
---|---|---|---|
1. | <- or = or <<- | These operators are known as left assignment operators. |
a <- c(3, 0, TRUE, 2+2i) b <<- c(2, 4, TRUE, 2+3i) d = c(1, 2, TRUE, 2+3i) print(a) print(b) print(d) It will give us the following output: [1] 3+0i 0+0i 1+0i 2+2i [1] 2+0i 4+0i 1+0i 2+3i [1] 1+0i 2+0i 1+0i 2+3i |
2. | -> or ->> | These operators are known as right assignment operators. |
c(3, 0, TRUE, 2+2i) -> a c(2, 4, TRUE, 2+3i) ->> b print(a) print(b) It will give us the following output: [1] 3+0i 0+0i 1+0i 2+2i [1] 2+0i 4+0i 1+0i 2+3i |
R支持的运算符:
其他运算符用于特殊目的。这些运算符不用于一般的数学或逻辑计算。 R中支持以下杂类运算符
S. No | Operator | Description | Example |
---|---|---|---|
1. | : | The colon operator is used to create the series of numbers in sequence for a vector. |
v <- 1:8 print(v) It will give us the following output: [1] 1 2 3 4 5 6 7 8 |
2. | %in% | This is used when we want to identify if an element belongs to a vector. |
a1 <- 8 a2 <- 12 d <- 1:10 print(a1%in%t) print(a2%in%t) It will give us the following output: [1] FALSE [1] FALSE |
3. | %*% | It is used to multiply a matrix with its transpose. |
M=matrix(c(1,2,3,4,5,6), nrow=2, ncol=3, byrow=TRUE) T=m%*%T(m) print(T) It will give us the following output: 14 32 32 77 |