R-operators
R-演算子
演算子は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。 R言語には組み込みの演算子が豊富で、次の種類の演算子が用意されています。
演算子の種類
Rプログラミングには次のタイプの演算子があります-
- 算術演算子
- 関係演算子
- 論理演算子
- 割り当て演算子
- その他の演算子
算術演算子
次の表に、R言語でサポートされている算術演算子を示します。 演算子は、ベクトルの各要素に作用します。
Operator | Description | Example |
---|---|---|
PLUS | Adds two vectors |
それは次の結果を生成します-
|
− | Subtracts second vector from the first |
それは次の結果を生成します-
|
* | Multiplies both vectors |
それは次の結果を生成します-
|
/ | Divide the first vector with the second |
上記のコードを実行すると、次の結果が生成されます-
|
%% | Give the remainder of the first vector with the second |
それは次の結果を生成します-
|
%/% | The result of division of first vector with second (quotient) |
それは次の結果を生成します-
|
^ | The first vector raised to the exponent of second vector |
それは次の結果を生成します-
|
関係演算子
次の表に、R言語でサポートされている関係演算子を示します。 最初のベクトルの各要素は、2番目のベクトルの対応する要素と比較されます。 比較の結果はブール値です。
Operator | Description | Example |
---|---|---|
> | Checks if each element of the first vector is greater than the corresponding element of the second vector. |
それは次の結果を生成します-
|
< | Checks if each element of the first vector is less than the corresponding element of the second vector. |
それは次の結果を生成します-
|
== | Checks if each element of the first vector is equal to the corresponding element of the second vector. |
それは次の結果を生成します-
|
⇐ | Checks if each element of the first vector is less than or equal to the corresponding element of the second vector. |
それは次の結果を生成します-
|
>= | Checks if each element of the first vector is greater than or equal to the corresponding element of the second vector. |
それは次の結果を生成します-
|
!= | Checks if each element of the first vector is unequal to the corresponding element of the second vector. |
それは次の結果を生成します-
|
論理演算子
次の表に、R言語でサポートされる論理演算子を示します。 論理型、数値型、または複素数型のベクトルにのみ適用できます。 1より大きいすべての数値は、論理値TRUEと見なされます。
最初のベクトルの各要素は、2番目のベクトルの対応する要素と比較されます。 比較の結果はブール値です。
Operator | Description | Example |
---|---|---|
& | It is called Element-wise Logical AND operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if both the elements are TRUE. |
それは次の結果を生成します-
|
It is called Element-wise Logical OR operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if one the elements is TRUE. | ||
|
t) ---- それは次の結果を生成します- [source,result,notranslate] ---- [1] TRUE FALSE TRUE TRUE ---- |
! |
論理演算子&&および||ベクトルの最初の要素のみを考慮し、出力として単一要素のベクトルを与えます。
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. Takes first element of both the vectors and gives the TRUE only if both are TRUE. |
それは次の結果を生成します-
|
Called Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE. |
|
割り当て演算子
これらの演算子は、ベクトルに値を割り当てるために使用されます。
Operator | Description | Example |
---|---|---|
<− or = or <<− |
Called Left Assignment |
それは次の結果を生成します-
a |
→ or →> |
Called Right Assignment |
それは次の結果を生成します-
|
その他の演算子
これらの演算子は、一般的な数学的または論理的な計算ではなく、特定の目的に使用されます。
Operator | Description | Example |
---|---|---|
: | Colon operator. It creates the series of numbers in sequence for a vector. |
それは次の結果を生成します-
|
%in% | This operator is used to identify if an element belongs to a vector. |
それは次の結果を生成します-
|
%*% | This operator is used to multiply a matrix with its transpose. |
それは次の結果を生成します-
|