R行列


マトリックス

行列は、列と行を持つ2次元のデータセットです。

列はデータの垂直方向の表現であり、行はデータの水平方向の表現です。

関数を使用して行列を作成できますmatrix()行と列の量を取得するには、nrowおよびパラメーターを指定します。ncol

# Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)

# Print the matrix
thismatrix

注:このc()関数は、アイテムを連結するために使用されることに注意してください。

文字列を使用してマトリックスを作成することもできます。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix

マトリックスアイテムへのアクセス

[ ] 角かっこを使用してアイテムにアクセスできます。括弧内の最初の数字「1」は行の位置を指定し、2番目の数字「2」は列の位置を指定します。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix[1, 2]

角かっこ内の数字のにコンマを指定すると、行全体にアクセスできます。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix[2,]

角かっこ内の数字のにコンマを指定すると、列全体にアクセスできます。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix[,2]

複数の行にアクセスする

c()次の関数を使用すると、複数の行にアクセスできます。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)

thismatrix[c(1,2),]

複数の列にアクセスする

c()次の関数を使用すると、複数の列にアクセスできます。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)

thismatrix[, c(1,2)]

行と列を追加する

この関数を使用してcbind()、マトリックスに列を追加します。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)

newmatrix <- cbind(thismatrix, c("strawberry", "blueberry", "raspberry"))

# Print the new matrix
newmatrix

注:新しい列のセルは、既存のマトリックスと同じ長さである必要があります。

この関数を使用rbind()して、マトリックスに行を追加します。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)

newmatrix <- rbind(thismatrix, c("strawberry", "blueberry", "raspberry"))

# Print the new matrix
newmatrix

注:新しい行のセルは、既存のマトリックスと同じ長さである必要があります。


行と列を削除する

関数を使用してc()、マトリックス内の行と列を削除します。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "mango", "pineapple"), nrow = 3, ncol =2)

#Remove the first row and the first column
thismatrix <- thismatrix[-c(1), -c(1)]

thismatrix

アイテムが存在するかどうかを確認します

指定されたアイテムがマトリックスに存在するかどうかを確認するには、%in%演算子を使用します。

「アップル」がマトリックスに存在するかどうかを確認します。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

"apple" %in% thismatrix

行と列の量

この関数を使用してdim()、マトリックス内の行と列の数を見つけます。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

dim(thismatrix)

マトリックスの長さ

関数を使用してlength()、行列の次元を見つけます。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

length(thismatrix)

マトリックス内の合計セルは、行数に列数を掛けたものです。

上記の例では、ディメンション= 2 * 2 = 4です。


マトリックスをループする

ループを使用してマトリックスをループできforます。ループは最初の行から始まり、右に移動します。

マトリックスアイテムをループして印刷します。

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

for (rows in 1:nrow(thismatrix)) {
  for (columns in 1:ncol(thismatrix)) {
    print(thismatrix[rows, columns])
  }
}

2つの行列を組み合わせる

ここでも、rbind()orcbind()関数を使用して、2つ以上の行列を組み合わせることができます。

# Combine matrices
Matrix1 <- matrix(c("apple", "banana", "cherry", "grape"), nrow = 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango", "pineapple", "watermelon"), nrow = 2, ncol = 2)

# Adding it as a rows
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined

# Adding it as a columns
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined