R-operators

提供:Dev Guides
移動先:案内検索

R-演算子

演算子は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。 R言語には組み込みの演算子が豊富で、次の種類の演算子が用意されています。

演算子の種類

Rプログラミングには次のタイプの演算子があります-

  • 算術演算子
  • 関係演算子
  • 論理演算子
  • 割り当て演算子
  • その他の演算子

算術演算子

次の表に、R言語でサポートされている算術演算子を示します。 演算子は、ベクトルの各要素に作用します。

Operator Description Example
PLUS Adds two vectors
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v&plus;t)

それは次の結果を生成します-

[1] 10.0  8.5  10.0
Subtracts second vector from the first
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)

それは次の結果を生成します-

[1] -6.0  2.5  2.0
* Multiplies both vectors
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)

それは次の結果を生成します-

[1] 16.0 16.5 24.0
/ Divide the first vector with the second
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)

上記のコードを実行すると、次の結果が生成されます-

[1] 0.250000 1.833333 1.500000
%% Give the remainder of the first vector with the second
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)

それは次の結果を生成します-

[1] 2.0 2.5 2.0
%/% The result of division of first vector with second (quotient)
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)

それは次の結果を生成します-

[1] 0 1 1
^ The first vector raised to the exponent of second vector
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)

それは次の結果を生成します-

[1]  256.000  166.375 1296.000

関係演算子

次の表に、R言語でサポートされている関係演算子を示します。 最初のベクトルの各要素は、2番目のベクトルの対応する要素と比較されます。 比較の結果はブール値です。

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)

それは次の結果を生成します-

[1] FALSE  TRUE FALSE FALSE
< Checks if each element of the first vector is less 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)

それは次の結果を生成します-

[1]  TRUE FALSE  TRUE FALSE
== Checks if each element of the first vector is 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)

それは次の結果を生成します-

[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.
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v<=t)

それは次の結果を生成します-

[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.
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>=t)

それは次の結果を生成します-

[1] FALSE  TRUE FALSE  TRUE
!= Checks if each element of the first vector is unequal 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)

それは次の結果を生成します-

[1]  TRUE  TRUE  TRUE FALSE

論理演算子

次の表に、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.
v <- c(3,1,TRUE,2&plus;3i)
t <- c(4,1,FALSE,2&plus;3i)
print(v&t)

それは次の結果を生成します-

[1]  TRUE  TRUE FALSE  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.
v <- c(3,0,TRUE,2&plus;2i)
t <- c(4,0,FALSE,2&plus;3i)
print(v

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.
v <- c(3,0,TRUE,2&plus;2i)
t <- c(1,3,TRUE,2&plus;3i)
print(v&&t)

それは次の結果を生成します-

[1] TRUE
Called Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE.
v <- c(0,0,TRUE,2&plus;2i)
t <- c(0,3,TRUE,2&plus;3i)
print(v

割り当て演算子

これらの演算子は、ベクトルに値を割り当てるために使用されます。

Operator Description Example

<−

or

=

or

<<−

Called Left Assignment
v1 <- c(3,1,TRUE,2&plus;3i)
v2 <<- c(3,1,TRUE,2&plus;3i)
v3 = c(3,1,TRUE,2&plus;3i)
print(v1)
print(v2)
print(v3)

それは次の結果を生成します-

[1] 3&plus;0i 1&plus;0i 1&plus;0i 2&plus;3i
[1] 3&plus;0i 1&plus;0i 1&plus;0i 2&plus;3i
[1] 3&plus;0i 1&plus;0i 1&plus;0i 2&plus;3i

a

or

→>

Called Right Assignment
c(3,1,TRUE,2&plus;3i) -> v1
c(3,1,TRUE,2&plus;3i) ->> v2
print(v1)
print(v2)

それは次の結果を生成します-

[1] 3&plus;0i 1&plus;0i 1&plus;0i 2&plus;3i
[1] 3&plus;0i 1&plus;0i 1&plus;0i 2&plus;3i

その他の演算子

これらの演算子は、一般的な数学的または論理的な計算ではなく、特定の目的に使用されます。

Operator Description Example
: Colon operator. It creates the series of numbers in sequence for a vector.
v <- 2:8
print(v)

それは次の結果を生成します-

[1] 2 3 4 5 6 7 8
%in% This operator is used to identify if an element belongs to a vector.
v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t)
print(v2 %in% t)

それは次の結果を生成します-

[1] TRUE
[1] FALSE
%*% This operator is used to multiply a matrix with its transpose.
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