R演算子


演算子

演算子は、変数と値の操作を実行するために使用されます。

以下の例では、+演算子を使用して2つの値を加算します。

10 + 5

Rは、演算子を次のグループに分割します。

  • 算術演算子
  • 代入演算子
  • 比較演算子
  • 論理演算子
  • その他の演算子

R算術演算子

算術演算子は、一般的な数学演算を実行するために数値とともに使用されます。

Operator Name Example Try it
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
^ Exponent x ^ y
%% Modulus (Remainder from division) x %% y
%/% Integer Division x%/%y

R代入演算子

代入演算子は、変数に値を代入するために使用されます。

my_var <- 3

my_var <<- 3

3 -> my_var

3 ->> my_var

my_var # print my_var

注: <<-はグローバルアサイナです。これについては、グローバル変数の章で詳しく説明します。

代入演算子の方向を変えることも可能です。

x <-3は3-> xに等しい



R比較演算子

比較演算子は、次の2つの値を比較するために使用されます。

Operator Name Example Try it
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

R論理演算子

論理演算子は、条件ステートメントを組み合わせるために使用されます。

Operator Description
& Element-wise Logical AND operator. It returns TRUE if both elements are TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
| Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE
|| Logical OR operator. It returns TRUE if one of the statement is TRUE.
! Logical NOT - returns FALSE if statement is TRUE

Rその他の演算子

その他の演算子は、データを操作するために使用されます。

Operator Description Example
: Creates a series of numbers in a sequence x <- 1:10
%in% Find out if an element belongs to a vector x %in% y
%*% Matrix Multiplication x <- Matrix1 %*% Matrix2

注:行列の乗算と行列については、後の章で詳しく説明します。