LoginSignup
9
13

More than 5 years have passed since last update.

R matrix入門

Last updated at Posted at 2017-07-15

Rのmatrix入門

matrixをつくる

データからmatrixを作る

  • matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
  • デフォルトは列優先でdataの数値が並べられる
  • ncol, nrowのどちらか、もしくは両方で指定する
x <- matrix(1:6, nrow=3)
x
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
x <- matrix(1:6, ncol=3)
x
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6

ゼロmatrixを作る

z <- matrix(0, nrow=2, ncol=3)
z
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0

単位行列を作る

u <- diag(3)
u
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

matrixの部分を取り出す

ベクトル、数値での指定

ベクトルで指定できる

x <- matrix(1:6, ncol=3)
x[2, 2:3]
## [1] 4 6

行だけ、列だけを取り出す

x <- matrix(1:6, ncol=3)
x[,2]
## [1] 3 4
x <- matrix(1:6, ncol=3)
x[1,]
## [1] 1 3 5

行名や列名で取り出す

x <- matrix(1:9, nrow=3)
colnames(x) <- c("a","b","c")
rownames(x) <- c("R","Python","Julia")
x
##        a b c
## R      1 4 7
## Python 2 5 8
## Julia  3 6 9
x[,"b"]
##      R Python  Julia 
##      4      5      6
x["Python",]
## a b c 
## 2 5 8

ある行や列だけを取り出すと、ベクトルになる。matrixを維持したければ、drop=FALSEをつける

x <- matrix(1:6, ncol=3)
x[,2,drop=FALSE]
##      [,1]
## [1,]    3
## [2,]    4

関数を用いた抽出

対角要素

x <- matrix(1:9, ncol=3)
diag(x)
## [1] 1 5 9

上三角要素、下三角要素も取り出せる

x <- matrix(1:9, ncol=3)
x[upper.tri(x,diag=TRUE)]
## [1] 1 4 5 7 8 9

matrixの情報の変更と取得

matrixの行名や列名をつける

適当なmatrixを作る

x <- matrix(1:9, ncol=3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

行名と列名をつける

colnames(x) <- c("a","b","c")
rownames(x) <- c("R","Python","Julia")
x
##        a b c
## R      1 4 7
## Python 2 5 8
## Julia  3 6 9

matrixの構造に関する情報を取得する

x <- matrix(1:6, ncol=3)
x
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6

行数の取得

nrow(x)
## [1] 2

列数の取得

ncol(x)
## [1] 3

行と列の一括取得

dim(x)
## [1] 2 3

行名の取得

x <- matrix(1:9, ncol=3)
colnames(x) <- c("a","b","c")
rownames(x) <- c("R","Python","Julia")
colnames(x)
## [1] "a" "b" "c"
rownames(x)
## [1] "R"      "Python" "Julia"

matrixの要素の統計的な情報を取得する

x <- matrix(1:9, nrow=3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

行の合計

rowSums(x)
## [1] 12 15 18

行の平均

rowMeans(x)
## [1] 4 5 6

列の合計

colSums(x)
## [1]  6 15 24

列の平均

colMeans(x)
## [1] 2 5 8

matrixの概要を列単位で知る

summary(x)
##        V1            V2            V3     
##  Min.   :1.0   Min.   :4.0   Min.   :7.0  
##  1st Qu.:1.5   1st Qu.:4.5   1st Qu.:7.5  
##  Median :2.0   Median :5.0   Median :8.0  
##  Mean   :2.0   Mean   :5.0   Mean   :8.0  
##  3rd Qu.:2.5   3rd Qu.:5.5   3rd Qu.:8.5  
##  Max.   :3.0   Max.   :6.0   Max.   :9.0

matrixの列数や行数を変える操作

matrix同士を結合させる

行数を一定にして結合

x <- matrix(1:9,nrow=3)
y <- matrix(21:26,nrow=3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
y
##      [,1] [,2]
## [1,]   21   24
## [2,]   22   25
## [3,]   23   26
#行を一定なので、column を bind する
cbind(x,y)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    4    7   21   24
## [2,]    2    5    8   22   25
## [3,]    3    6    9   23   26

列数を一定にして結合

x <- matrix(1:9, ncol=3)
y <- matrix(21:26, ncol=3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
y
##      [,1] [,2] [,3]
## [1,]   21   23   25
## [2,]   22   24   26

列を一定なので row を bind する

rbind(x, y)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## [4,]   21   23   25
## [5,]   22   24   26

matrixに列や行を追加する

x <- matrix(1:9, nrow=3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

列を追加する

v <- c(1:3)
cbind(x, v)
##            v
## [1,] 1 4 7 1
## [2,] 2 5 8 2
## [3,] 3 6 9 3

行を追加する

rbind(x, v)
##   [,1] [,2] [,3]
##      1    4    7
##      2    5    8
##      3    6    9
## v    1    2    3

matrixの演算

matrix / vector は行数とベクトル要素数が同じならば、ベクトル各要素で各行を割る動作になる 具体例をみたらよくわかる

x <- matrix(1, nrow=4, ncol=3)
x
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    1    1
## [3,]    1    1    1
## [4,]    1    1    1
v <- c(1:4)
x / v
##           [,1]      [,2]      [,3]
## [1,] 1.0000000 1.0000000 1.0000000
## [2,] 0.5000000 0.5000000 0.5000000
## [3,] 0.3333333 0.3333333 0.3333333
## [4,] 0.2500000 0.2500000 0.2500000
9
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
13