logo

R – 行列

R行列 データを行と列に 2 次元に配置したものです。

マトリックスでは、行は水平に走るもの、列は垂直に走るものです。で Rプログラミング 、行列は 2 次元の同種のデータ構造です。以下に行列の例をいくつか示します。



R - 行列オタクフォーギーク

R – 行列

R で行列を作成する

R で行列を作成するには、次の関数を使用する必要があります。 マトリックス()

これに対する議論 マトリックス() はベクトル内の要素のセットです。行列に含める行数と列数を渡す必要があります。



注記: デフォルトでは、行列は列方向の順序になっています。

R 行列を作成するための構文

行列(データ、nrow、ncol、byrow、dimnames)

パラメーター:



  • データ - 入力したい値
  • 狭い – いいえ。行数
  • ncol – いいえ。列の
  • 付記 – 論理的な手がかり、「true」値が行によって割り当てられるかどうか
  • ディムネーム – 行と列の名前

例:

Windowsコマンドarp

R




# R program to create a matrix> > A =>matrix>(> > ># Taking sequence of elements> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> > ># No of rows> >nrow = 3,> > ># No of columns> >ncol = 3,> > ># By default matrices are in column-wise order> ># So this parameter decides how to arrange the matrix> >byrow =>TRUE> )> > # Naming rows> rownames>(A) =>c>(>'a'>,>'b'>,>'c'>)> > # Naming columns> colnames>(A) =>c>(>'c'>,>'d'>,>'e'>)> > cat>(>'The 3x3 matrix: '>)> print>(A)>

>

>

出力

The 3x3 matrix: c d e a 1 2 3 b 4 5 6 c 7 8 9>

R での特殊行列の作成

R では、matrix() 関数に渡される引数を使用して、さまざまな種類の行列を作成できます。

1. すべての行と列が単一の定数「k」で満たされる行列:

このような R 行列を作成するための構文は次のとおりです。

構文: 行列(k, m, n)

パラメーター:
k: 定数
メートル: 行数
n: 列の数

例:

R




# R program to illustrate> # special matrices> # Matrix having 3 rows and 3 columns> # filled by a single constant 5> print>(>matrix>(5, 3, 3))>

>

>

出力

 [,1] [,2] [,3] [1,] 5 5 5 [2,] 5 5 5 [3,] 5 5 5>

2. 対角行列:

対角行列は、主対角の外側の要素がすべて 0 である行列です。このような R 行列を作成するための構文は次のとおりです。

構文: diag(k, m, n)
パラメーター:
k: 定数/配列
メートル: 行数
n: 列の数

例:

R




# R program to illustrate> # special matrices> # Diagonal matrix having 3 rows and 3 columns> # filled by array of elements (5, 3, 3)> print>(>diag>(>c>(5, 3, 3), 3, 3))>

>

>

出力

 [,1] [,2] [,3] [1,] 5 0 0 [2,] 0 3 0 [3,] 0 0 3>

3. 恒等行列:

主対角のすべての要素が 1 であり、他のすべての要素が 0 である単位行列。このような R 行列を作成するための構文は次のとおりです。

構文: diag(k, m, n)
パラメーター:
k: 1
メートル: 行数
n: 列の数

例:

R




# R program to illustrate> # special matrices> # Identity matrix having> # 3 rows and 3 columns> print>(>diag>(1, 3, 3))>

>

>

出力

 [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1>

4. マトリックスメトリクス

マトリックス メトリックは、作成したマトリックスについて示します。行列の行数、列数、次元を知りたい場合があります。

jsonファイルを読む

以下の例は、次の質問に答えるのに役立ちます。

  • 行列の次元はどのようにして知ることができるのでしょうか?
  • 行列に行が何行あるかをどうやって知ることができるでしょうか?
  • マトリックスには何列ありますか?
  • マトリックスにはいくつの要素がありますか?

例:

R




# R program to illustrate> # matrix metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> cat>(>'Dimension of the matrix: '>)> print>(>dim>(A))> cat>(>'Number of rows: '>)> print>(>nrow>(A))> cat>(>'Number of columns: '>)> print>(>ncol>(A))> cat>(>'Number of elements: '>)> print>(>length>(A))> # OR> print>(>prod>(>dim>(A)))>

>

>

出力

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Dimension of the matrix: [1] 3 3 Number of rows: [1] 3 Number of columns: [1] 3 Number of elements: [1] ...>

R 行列の要素へのアクセス

データ フレームで従うのと同じ規則を使用して、R 行列の要素にアクセスできます。つまり、行列の後に、配列の間にカンマが入った角括弧が続きます。

カンマの前の値は行へのアクセスに使用され、カンマの後の値は列へのアクセスに使用されます。簡単な R コードを取り上げてこれを説明しましょう。

行へのアクセス:

R




# R program to illustrate> # access rows in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> # Accessing first and second row> cat>(>'Accessing first and second row '>)> print>(A[1:2, ])>

>

>

出力

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing first and second row [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6>

列へのアクセス:

R




# R program to illustrate> # access columns in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> # Accessing first and second column> cat>(>'Accessing first and second column '>)> print>(A[, 1:2])>

>

>

出力

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing first and second column [,1] [,2] [1,] 1 2 [2,] 4 5 [3,] 7 8>

R 行列の要素にアクセスするその他の例:

R




# R program to illustrate> # access an entry in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> # Accessing 2> print>(A[1, 2])> # Accessing 6> print>(A[2, 3])>

>

>

出力

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [1] 2 [1] 6>

R で部分行列にアクセスする:

行列内の部分行列には、 結腸(:) オペレーター。

R




シンアルゴリズム
# R program to illustrate> # access submatrices in a matrix> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> cat>(>'Accessing the first three rows and the first two columns '>)> print>(A[1:3, 1:2])>

>

>

出力

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing the first three rows and the first two columns [,1] [,2] [1,] 1 2 [2,] 4 5 [3...>

R 行列の要素の変更

R では、直接代入によって行列の要素を変更できます。

例:

R




# R program to illustrate> # editing elements in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> # Editing the 3rd rows and 3rd column element> # from 9 to 30> # by direct assignments> A[3, 3] = 30> cat>(>'After edited the matrix '>)> print>(A)>

>

>

出力

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After edited the matrix [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 30>

R 行列の連結

行列の連結とは、既存の R 行列の行または列を結合することを指します。

行の連結:

行列への行の連結は、次を使用して行われます。 rbind()

R




# R program to illustrate> # concatenation of a row in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> # Creating another 1x3 matrix> B =>matrix>(> >c>(10, 11, 12),> >nrow = 1,> >ncol = 3> )> cat>(>'The 1x3 matrix: '>)> print>(B)> # Add a new row using rbind()> C =>rbind>(A, B)> cat>(>'After concatenation of a row: '>)> print>(C)>

>

10/10
>

出力

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 The 1x3 matrix: [,1] [,2] [,3] [1,] 10 11 12 After concatenation of a row: [,1] [,2] [,3...>

列の連結:

列と行列の連結は、次を使用して行われます。 cbind()

R




# R program to illustrate> # concatenation of a column in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> # Creating another 3x1 matrix> B =>matrix>(> >c>(10, 11, 12),> >nrow = 3,> >ncol = 1,> >byrow =>TRUE> )> cat>(>'The 3x1 matrix: '>)> print>(B)> # Add a new column using cbind()> C =>cbind>(A, B)> cat>(>'After concatenation of a column: '>)> print>(C)>

>

>

出力

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 The 3x1 matrix: [,1] [1,] 10 [2,] 11 [3,] 12 After concatenation of a column: [,1] [,2] ...>

寸法の不一致: この行列の連結を行う前に、行列間の次元の一貫性を確認する必要があることに注意してください。

R




# R program to illustrate> # Dimension inconsistency in metrics concatenation> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix: '>)> print>(A)> # Creating another 1x3 matrix> B =>matrix>(> >c>(10, 11, 12),> >nrow = 1,> >ncol = 3,> )> cat>(>'The 1x3 matrix: '>)> print>(B)> # This will give an error> # because of dimension inconsistency> C =>cbind>(A, B)> cat>(>'After concatenation of a column: '>)> print>(C)>

>

>

出力:

The 3x3 matrix:  [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 The 1x3 matrix:  [, 1] [, 2] [, 3] [1, ] 10 11 12 Error in cbind(A, B) : number of rows of matrices must match (see arg 2)>

R 行列に行と列を追加する

R 行列に行を追加するには、次を使用できます rbind() 関数を使用し、R 行列に列を追加するには、次のように使用できます。 cバインド () 関数。

行の追加

R 行列に行を追加する方法について、以下の例を見てみましょう。

例:

R




# Create a 3x3 matrix> number <->matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'Before inserting a new row: '>)> print>(number)> # New row to be inserted> new_row <->c>(10, 11, 12)># Define the new row> # Inserting the new row at the second position> A <->rbind>(number[1, ], new_row, number[-1, ])> cat>(>' After inserting a new row: '>)> print>(number)>

>

>

出力

Before inserting a new row: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After inserting a new row: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,]...>

列の追加

R マトリックスに列を追加する方法について、以下の例を見てみましょう。

R




# Create a 3x3 matrix> number <->matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'Before adding a new column: '>)> print>(number)> # New column to be added> new_column <->c>(10, 11, 12)># Define the new column> # Adding the new column at the end> number <->cbind>(number, new_column)> cat>(>' After adding a new column: '>)> print>(number)>

>

>

出力

Before adding a new column: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After adding a new column: new_column [1,] 1 2 3 10 [2,] 4 5 6 1...>

R 行列の行と列の削除

行または列を削除するには、まずその行または列にアクセスし、その行または列の前にマイナス記号を挿入する必要があります。これは、その行または列を削除する必要があったことを示します。

行の削除:

R




CSSの最初の子

# R program to illustrate> # row deletion in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'Before deleting the 2nd row '>)> print>(A)> # 2nd-row deletion> A = A[-2, ]> cat>(>'After deleted the 2nd row '>)> print>(A)>

>

>

出力

Before deleting the 2nd row [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After deleted the 2nd row [,1] [,2] [,3] [1,] 1 2 3 [2,] 7 8 9>

列の削除:

R




# R program to illustrate> # column deletion in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'Before deleting the 2nd column '>)> print>(A)> # 2nd-row deletion> A = A[, -2]> cat>(>'After deleted the 2nd column '>)> print>(A)>

>

>

出力

Before deleting the 2nd column [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After deleted the 2nd column [,1] [,2] [1,] 1 3 [2,] 4 6 [3,] 7 9>

R 行列と、新しい行と列の追加、行と列の削除、行列の結合などの基本操作について説明しました。

これが R の行列の理解に役立ち、プロジェクトで R 行列を快適に使用できるようになることを願っています。

以下もチェックしてください: