r語言 運算符
R語言中的運算符 (Operators in R Language)
Generally speaking, an operator is a symbol that gives proper commands to the compiler regarding a specific action to be executed. The operators are used for carrying out the mathematical or logical calculations or also sometimes manipulations are also done using these operators. R language is normally considered as the rich language with several built-in operators to make a programmer more comfortable while coding the program. Moving further let us find what all are the inbuilt operators that are offered by the R language to its users.
一般來說,運算符是一個符號,它向編譯器提供有關要執行的特定操作的適當命令。 運算符用于執行數學或邏輯計算,或者有時也使用這些運算符進行操作。 R語言通常被認為是具有多種內置運算符的豐富語言,可以使程序員在編寫程序時更加舒適。 進一步講,我們可以找到R語言為其用戶提供的所有內置運算符。
Concerning other programming languages like C, C++, Java, etc, here also we find such similar operators that help the coders while coding their program. The commonly used operators are as follows:
關于其他編程語言(例如C,C ++,Java等),在這里我們也找到了類似的運算符,可在編碼程序時幫助編碼人員。 常用的運算符如下:
Arithmetic operators
算術運算符
Relational Operators
關系運算符
Logical Operators
邏輯運算符
Assignment operators
賦值運算符
Miscellaneous operators
雜項運營商
1)算術運算符 (1) Arithmetic Operators)
The operators like +, -, *, /, %%, %/%, ^ fall under the category of the arithmetic operators. The main usage of each operator is explained in details in the following text,
像+,-,*,/,%%,%/%,^之類的運算符屬于算術運算符的類別。 下文詳細說明了每個運算符的主要用法,
Plus Operator (+): This operator helps in adding the two vectors.
加號運算符(+) :此運算符有助于將兩個向量相加。
Example:
例:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)
Output
輸出量
[1] 10.0 8.5 10.0
Minus Operator (-): This particular operator subtracts the second vector from the first one.
減號(-) :此特定運算符從第一個向量中減去第二個向量。
Example:
例:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)
Output
輸出量
[1] -6.0 2.5 2.0
Multiply Operator (*): It multiplies both vectors.
乘運算符(*) :將兩個向量相乘。
Example:
例:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)
Output
輸出量
[1] 16.0 16.5 24.0
Divide Operator (/): It mainly divides the first vector considered with the second one taken.
除法運算符(/) :主要將考慮的第一個向量除以第二個向量。
Example:
例:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)
Output
輸出量
[1] 0.250000 1.833333 1.500000
Remainder Operator (%%): It gives the remainder of the first vector that is considered with the second one.
余數運算符(%%) :它給出與第二個向量一起考慮的第一個向量的余數。
Example:
例:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)
Output
輸出量
[1] 2.0 2.5 2.0
Division Operator (%/%): The result of the division of the first vector to the second one(quotient).
除法運算符(%/%) :第一個向量除以第二個向量(商)的結果。
Example:
例:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)
Output
輸出量
[1] 0 1 1
Raised to the Power (^): The first vector raised to the second one.
提高到冪(^) :第一個矢量提高到第二個。
Example:
例:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)
Output
輸出量
[1] 256.000 166.375 1296.000
2)關系運算符 (2) Relational Operators)
This is one of the sub-branches under the operators available in the R language. The main use of these operators is to find the relation between the two vectors considered.
這是R語言中可用的運算符的子分支之一。 這些運算符的主要用途是找到所考慮的兩個向量之間的關系。
The result of these operators is usually a boolean value.
這些運算符的結果通常是布爾值。
Coming to the list of available relational operators in the R language are as follows:
R語言中可用的關系運算符列表如下:
Greater Than | > |
Less Than | < |
Equal To | == |
Greater Than or Equal To | >= |
Less Than or Equal To | <= |
Not Equal To | != |
比...更棒 | > |
少于 | < |
等于 | == |
大于或等于 | > = |
小于或等于 | <= |
不等于 | != |
The above operators have their own meanings while executing the program.
上述運算符在執行程序時具有各自的含義。
Greater Than Operator (>)
大于運算符(>)
The above operator helps the user in the process of determining whether the first number is greater than the second one.
上述運算符在確定第一數字是否大于第二數字的過程中幫助用戶。
Example:
例:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)
Output
輸出量
[1] FALSE TRUE FALSE FALSE
Less Than Operator (
少于運算符(
This operator tells the user whether the first operator is less than the second one.
該運算符告訴用戶第一個運算符是否小于第二個運算符。
Example:
例:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v < t)
Output
輸出量
[1] TRUE FALSE TRUE FALSE
Equal To Operator (==)
等于運算符(==)
Finds whether the both values are same to each other or not.
查找兩個值是否彼此相同。
Example:
例:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v == t)
Output
輸出量
[1] FALSE FALSE FALSE TRUE
Greater Than or Equal To Operator (>=)
大于或等于運算符(> =)
Confirms whether each component of the foremost vector is superior than or equivalent to the corresponding part of the subsequent vector.
確認最前向量的每個分量是否優于或等效于后續向量的相應部分。
Example:
例:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>=t)
Output
輸出量
[1] FALSE TRUE FALSE TRUE
Less Than or Equal To Operator (<=)
小于或等于運算符(<=)
Verifies if each component of the initial vector is less than or equal to the equivalent element of the next vector.
驗證初始向量的每個分量是否小于或等于下一個向量的等效元素。
Example:
例:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v<=t)
Output
輸出量
[1] TRUE FALSE TRUE TRUE
Not Equal To Operator (!=)
不等于運算符(!=)
Verifies if each component of the initial vector is not equal to the equivalent element of the following vector.
驗證初始向量的每個分量是否不等于后續向量的等效元素。
Example:
例:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v!=t)
Output
輸出量
[1] TRUE TRUE TRUE FALSE
3)邏輯運算符 (3) Logical Operators)
The following table shows all the available logical operators in the R language. The below operators are applicable over the vectors which are complex, logical or numeric-only. Here the R compiled assumes that all the numbers that are greater than the one are assumed to possess a logic value true.
下表顯示了R語言中所有可用的邏輯運算符。 以下運算符適用于復雜,僅邏輯或僅數字的向量。 在此,R編譯器假定所有大于一個的數字均具有邏輯值true。
Here while the logical operators are considered then every element available in the first vector is usually compared with the corresponding next element of the second vector. Thus, the result of the above comparison gives us a Boolean value.
在這里,在考慮邏輯運算符的同時,通常將第一向量中的每個可用元素與第二向量中的對應的下一個元素進行比較。 因此,以上比較的結果給出了一個布爾值。
The logical operators offered by the R language are as follows:
R語言提供的邏輯運算符如下:
Element-wise Logical AND operator | & |
Element-wise Logical OR operator | | |
Logical NOT operator | ! |
Logical AND operator | && |
Logical OR operator | || |
逐元素邏輯AND運算符 | 和 |
按元素邏輯或運算符 | | |
邏輯非運算符 | ! |
邏輯AND運算子 | && |
邏輯或運算符 | || |
Element-wise Logical AND operator (&)
逐元素邏輯AND運算符(&)
It is named as the Element-wise Logical AND operator. It combines each component of the primary vector with the equivalent part of the next vector and gives an output TRUE if both the fundamental aspects are TRUE.
它被稱為Element-wise Logical AND運算符 。 如果兩個基本方面都為TRUE,它將主要向量的每個分量與下一個向量的等效部分合并,并給出輸出TRUE。
Example:
例:
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
Output
輸出量
[1] TRUE TRUE FALSE TRUE
Element-wise Logical OR operator (|)
逐元素邏輯或運算符(|)
It is termed as Element-wise Logical OR operator. It combines each element of the primary vector with the matching element of the subsequent vector and gives an output TRUE if one of the elements is TRUE.
它被稱為元素智能邏輯或運算符 。 它將主向量的每個元素與后續向量的匹配元素組合在一起,如果其中一個元素為TRUE,則輸出為TRUE。
Example:
例:
v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)
Output
輸出量
[1] TRUE FALSE TRUE TRUE
Logical NOT operator (!)
邏輯非運算符(!)
It is usually called with the name Logical NOT operator. The main function of this operator is that it takes each component of the vector and gives the contradictory logical value.
通常使用名稱Logical NOT運算符進行調用。 該運算符的主要功能是獲取矢量的每個分量并給出矛盾的邏輯值。
Example:
例:
v <- c(3,0,TRUE,2+2i)
print(!v)
Output
輸出量
[1] FALSE TRUE FALSE FALSE
Logical AND operator (&&)
邏輯AND運算符(&&)
Named as Logical AND operator. It takes the initial component of the first vector and compares it with the second one and finally gives the TRUE value if and only if both are TRUE.
命名為邏輯AND運算符 。 它采用第一個向量的初始分量,并將其與第二個向量進行比較,并且當且僅當兩者均為TRUE時,才給出TRUE值。
Example:
例:
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)
Output
輸出量
[1] TRUE
Logical OR operator (||)
邏輯或運算符(||)
It is termed as the Logical OR operator. The function of this operator is that it considers the primary components of both the vectors and eventually returns the value as TRUE if and only if one of them turns out to be TRUE.
它被稱為邏輯或運算符 。 該運算符的功能是,它考慮兩個向量的主要成分,并且僅當其中一個結果為TRUE時,才最終將值返回TRUE。
Example:
例:
v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)
Output
輸出量
[1] FALSE
4)賦值運算符 (4) Assignment Operators)
The main intention of using these assignment operators in the program is to assign some value to the variables or vectors declared.
在程序中使用這些賦值運算符的主要目的是為聲明的變量或向量賦值。
There are two ways while assigning the vectors is considered in the R language. They are:
在R語言中,可以采用兩種方式分配向量。 他們是:
Right assignment
正確的分配
Left assignment
左分配
Right assignment:
正確分配:
The operators used for right assignment are as follows:
用于權限分配的運算符如下:
=
=
<
<
Example:
例:
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)
Output
輸出量
[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
Left assignment:
?左分配:
Similarly, the operators used while implementing the left assignment technique is as follows:
同樣,實現左分配技術時使用的運算符如下:
->
->
->>
->>
Example:
例:
c(3,1,TRUE,2+3i) -> v1
c(3,1,TRUE,2+3i) ->> v2
print(v1)
print(v2)
Output
輸出量
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
5)雜項運營商 (5) Miscellaneous Operators)
These miscellaneous operators have their own specific purpose of usage in the program. They do not deal with either the logical or mathematical calculations or computations.
這些其他運算符在程序中有其特定的使用目的。 它們不處理邏輯或數學計算或計算。
Operator | Description | Example |
: | Colon operator: This operator creates the series of numerical in sequence for a considered vector. |
Output |
%in% | This operator is mainly used to recognize if an element/component belongs to the particular vector that is considered. |
Output |
%*% | This particular operator is used in the case when the multiplication of a matrix with its transpose is required to be performed. |
Output |
操作員 | 描述 | 例 |
: | 冒號運算符 :此運算符為考慮的向量順序創建一系列數值。 |
輸出量 |
%在% | 該運算符主要用于識別元素/組件是否屬于所考慮的特定向量。 |
輸出量 |
%*% | 在需要執行矩陣與其轉置相乘的情況下,使用此特定運算符。 |
輸出量 |
翻譯自: https://www.includehelp.com/r/operators.aspx
r語言 運算符