R Markdown for

Unit 08: 多維度資料格式

矩陣 – matrix

建立矩陣與性質

A <- matrix( c( 1, -5, 4, 3, 6, -2 ), nrow = 2, ncol = 3 )
# by column
A
##      [,1] [,2] [,3]
## [1,]    1    4    6
## [2,]   -5    3   -2
B <- matrix( c( 1, -5, 4, 3, 6, -2 ), nrow = 2, ncol = 3, byrow = TRUE)
# by row
B
##      [,1] [,2] [,3]
## [1,]    1   -5    4
## [2,]    3    6   -2

看看 A 是什麼類型

class(A)
## [1] "matrix" "array"

看看 A 的維度

dim(A)
## [1] 2 3

看看 A 的對應屬性與參數

attributes(A)
## $dim
## [1] 2 3

看看 A 的 橫列數量

nrow(A)
## [1] 2

看看 A 的 橫列數量

dim(A)[1]
## [1] 2

看看 A 的 直行數量

ncol(A)
## [1] 3

看看 A 的 直行數量

dim(A)[2]
## [1] 3

矩陣與向量

A <- matrix( c( 1, -5, 4, 3, 6, -2 ), nrow = 2, ncol = 3 )
A
##      [,1] [,2] [,3]
## [1,]    1    4    6
## [2,]   -5    3   -2

A 的內容物的特性

u <- as.numeric(A)
u
## [1]  1 -5  4  3  6 -2

A 的內容物的特性

v <- c(A)
v
## [1]  1 -5  4  3  6 -2

A 的內容物的特性的參數數據

dim(v)
## NULL
length(v)
## [1] 6
nrow(A) * ncol(A)
## [1] 6
length(A)
## [1] 6

零矩陣與單位矩陣

matrix( 0, nrow = 2, ncol = 3 )
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
matrix( 0, nrow = 3, ncol = 3 )
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
## [3,]    0    0    0
diag( 0, nrow = 3 )
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
## [3,]    0    0    0
diag( 3 )
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
diag( 2.5, nrow = 3 )
##      [,1] [,2] [,3]
## [1,]  2.5  0.0  0.0
## [2,]  0.0  2.5  0.0
## [3,]  0.0  0.0  2.5
diag( c(1, 2, 3), nrow = 3 )
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    2    0
## [3,]    0    0    3
diag( c(1, 2, 3) )
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    2    0
## [3,]    0    0    3

矩陣的 加 減 乘

A <- matrix( 1:9, nrow = 3, ncol = 3 )

B <- matrix( 1:9, nrow = 3, ncol = 3, byrow = TRUE )

A + B
##      [,1] [,2] [,3]
## [1,]    2    6   10
## [2,]    6   10   14
## [3,]   10   14   18
A - B
##      [,1] [,2] [,3]
## [1,]    0    2    4
## [2,]   -2    0    2
## [3,]   -4   -2    0
A %*% B
##      [,1] [,2] [,3]
## [1,]   66   78   90
## [2,]   78   93  108
## [3,]   90  108  126
A * B
##      [,1] [,2] [,3]
## [1,]    1    8   21
## [2,]    8   25   48
## [3,]   21   48   81

用指標的方式取出某個分量

A <- matrix( 1:9, nrow = 3, ncol = 3 )

B <- matrix( 1:9, nrow = 3, ncol = 3, byrow = TRUE )

A
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
B
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
A[2, 3]
## [1] 8
A[1, 2]
## [1] 4
A[4]
## [1] 4
c(A)[4]
## [1] 4
R1 <- A[1,]
R1
## [1] 1 4 7
class(R1)
## [1] "integer"
R1[1]
## [1] 1
R1 * B
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   16   20   24
## [3,]   49   56   63
R1 %*% B
##      [,1] [,2] [,3]
## [1,]   66   78   90

成為一個矩陣

A <- matrix( 1:9, nrow = 3, ncol = 3 )
B <- matrix( 1:9, nrow = 3, ncol = 3, byrow = TRUE )
A
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
B
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
R2 <- A[ 1, , drop = FALSE ]

R2[1, 2]
## [1] 4
R2 %*% B
##      [,1] [,2] [,3]
## [1,]   66   78   90
as.matrix(R1)
##      [,1]
## [1,]    1
## [2,]    4
## [3,]    7
R2
##      [,1] [,2] [,3]
## [1,]    1    4    7
R1
## [1] 1 4 7
R4 <- A[ , 1]
R4
## [1] 1 2 3
R4 <- A[ , 1:2]
R4
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
R2 <- A[ 1, , drop = FALSE ]
R2[1, 2]
## [1] 4
R2
##      [,1] [,2] [,3]
## [1,]    1    4    7
R2 <- A[ 1, ]
R2
## [1] 1 4 7
R2 <- A[ 1, , drop = FALSE]
R2
##      [,1] [,2] [,3]
## [1,]    1    4    7
R1 <- A[ 1, ]
class(R1)
## [1] "integer"
R2 <- A[ 1,  , drop = FALSE ]
class( R2 )
## [1] "matrix" "array"

用指標的方式取出某個分量

A <- matrix( 1:9, nrow = 3, ncol = 3 )
B <- matrix( 1:9, nrow = 3, ncol = 3, byrow = TRUE )
A
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
B
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
A[2, 3]
## [1] 8
A[1, 2]
## [1] 4
A[4]
## [1] 4
c(A)[4]
## [1] 4
R1 <- A[ 1, ]

class(R1)
## [1] "integer"
R1[ 1 ]
## [1] 1
R1 * B
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   16   20   24
## [3,]   49   56   63
R1 %*% B
##      [,1] [,2] [,3]
## [1,]   66   78   90

成為一個矩陣

A <- matrix( 1:9, nrow = 3, ncol = 3 )
B <- matrix( 1:9, nrow = 3, ncol = 3, byrow = TRUE )
A
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
B
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
R2 <- A[ 1,  , drop = FALSE ]

class( R2 )
## [1] "matrix" "array"
R2[ 1, 2 ]
## [1] 4
R2 %*% B
##      [,1] [,2] [,3]
## [1,]   66   78   90

形成另外一個矩陣或向量

A <- matrix( 1:9, nrow = 3, ncol = 3 )

E <- A[ c(1, 3), ]
class( E )
## [1] "matrix" "array"
E
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    3    6    9
F <- A[ c(1, 3), 2 ]
class( F )
## [1] "integer"
F
## [1] 4 6

結合矩陣

C <- matrix( 1:4, nrow = 2, ncol = 2 )
D <- matrix( 1:6, nrow = 2, ncol = 3 )
C
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
D
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
cbind( C, D )
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    1    3    5
## [2,]    2    4    2    4    6
E <- matrix( 1:4, nrow = 2, ncol = 2 )
F <- matrix( 1:6, nrow = 3, ncol = 2 )
E
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
F
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
rbind(E, F)
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## [3,]    1    4
## [4,]    2    5
## [5,]    3    6

轉置矩陣

A <- matrix( 1:9, nrow = 3, ncol = 3 )
A
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
t( A )
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
t( A ) %*% A
##      [,1] [,2] [,3]
## [1,]   14   32   50
## [2,]   32   77  122
## [3,]   50  122  194
diag( A )
## [1] 1 5 9
sum( diag( A ) )
## [1] 15

矩陣的行列式值與反矩陣

A <- matrix( c(1, 0, 0, 3, 0.5, 0, 2, 1, 0.25), nrow = 3, ncol = 3 )
A
##      [,1] [,2] [,3]
## [1,]    1  3.0 2.00
## [2,]    0  0.5 1.00
## [3,]    0  0.0 0.25
det(A)
## [1] 0.125
Ainv <- solve(A)
Ainv
##      [,1] [,2] [,3]
## [1,]    1   -6   16
## [2,]    0    2   -8
## [3,]    0    0    4
Ainv %*% A
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
A <- matrix( c(1, 0, 0, 3, 0.5, 0, 2, 1, 0.25), nrow = 3, ncol = 3 )
A
##      [,1] [,2] [,3]
## [1,]    1  3.0 2.00
## [2,]    0  0.5 1.00
## [3,]    0  0.0 0.25
b <- c(2, 1, 3)
b
## [1] 2 1 3
solve( A, b )
## [1]  44 -22  12
A
##      [,1] [,2] [,3]
## [1,]    1  3.0 2.00
## [2,]    0  0.5 1.00
## [3,]    0  0.0 0.25
b
## [1] 2 1 3

陣列 – array

array( 1:12 )
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12
array(  , c(3, 4) )
##      [,1] [,2] [,3] [,4]
## [1,]   NA   NA   NA   NA
## [2,]   NA   NA   NA   NA
## [3,]   NA   NA   NA   NA
array( 1:12, c(3, 4) )
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
array( data = 1:12, dim = c(3, 4) )
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
array( data = 1:60, dim = c(3, 4, 5) )
## , , 1
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24
## 
## , , 3
## 
##      [,1] [,2] [,3] [,4]
## [1,]   25   28   31   34
## [2,]   26   29   32   35
## [3,]   27   30   33   36
## 
## , , 4
## 
##      [,1] [,2] [,3] [,4]
## [1,]   37   40   43   46
## [2,]   38   41   44   47
## [3,]   39   42   45   48
## 
## , , 5
## 
##      [,1] [,2] [,3] [,4]
## [1,]   49   52   55   58
## [2,]   50   53   56   59
## [3,]   51   54   57   60
args( array )
## function (data = NA, dim = length(data), dimnames = NULL) 
## NULL

列表

camera <- list(c("Leica", "Pentax", "Olympus", "Nikon"), c(1.2, 3.4), c("red", "green", "blue"))

camera
## [[1]]
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"  
## 
## [[2]]
## [1] 1.2 3.4
## 
## [[3]]
## [1] "red"   "green" "blue"
camera <- list(brand = c("Leica", "Pentax", "Olympus", "Nikon"), real.number = c(1.2, 3.4), color = c("red", "green", "blue"))

camera
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"  
## 
## $real.number
## [1] 1.2 3.4
## 
## $color
## [1] "red"   "green" "blue"

索引方式 1 – 使用 [ ]

a1 <- camera[ 1 ]
a1
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
camera[ "brand" ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
class( a1 )
## [1] "list"

#索引方式 2 – 使用 [[ ]]

a2 <- camera[[ 1 ]]
a2
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
camera[[ "brand" ]]
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
class( a2 )
## [1] "character"

索引方式 3 – 使用 $

a3 <- camera$brand
a3
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
class( a3 )
## [1] "character"

索引方式 的比較

a1              # a1 是列表
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a2              # a2 是向量
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a3              # a3 是向量
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
class( a1 )
## [1] "list"
class( a2 )
## [1] "character"
class( a3 )
## [1] "character"
camera[ 1 ][ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a1[ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a2[ c(1, 2) ]
## [1] "Leica"  "Pentax"
a3[ 2 ]
## [1] "Pentax"
a1
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a1[1]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a1[ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
camera[ 1 ][ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a1[ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a2[ c(1, 2) ]
## [1] "Leica"  "Pentax"
a3[ 2 ]
## [1] "Pentax"
camera[ 1 ][ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a1[ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a2[ c(1, 2) ]
## [1] "Leica"  "Pentax"
a3[ 2 ]
## [1] "Pentax"
a1[ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a2[ c(1, 2) ]
## [1] "Leica"  "Pentax"
a3[ 2 ]
## [1] "Pentax"
a1              # a1 是列表
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a2              # a2 是向量
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a3              # a3 是向量
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
class( a1 )
## [1] "list"
class( a2 )
## [1] "character"
class( a3 )
## [1] "character"
camera[ 1 ][ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a1[ 1 ]
## $brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
a2[ c(1, 2) ]
## [1] "Leica"  "Pentax"
a3[ 2 ]
## [1] "Pentax"

資料框

x1 <- c("father", "mother", "brother", "sister")
x2 <- c("Leica", "Pentax", "Olympus", "Nikon")
x3 <- c("gold", "red", "green", "blue")
x4 <- c(2, 1, 1, 2)
camera <- data.frame(member = x1, brand = x2, color = x3, amount = x4)
camera
##    member   brand color amount
## 1  father   Leica  gold      2
## 2  mother  Pentax   red      1
## 3 brother Olympus green      1
## 4  sister   Nikon  blue      2
x1
## [1] "father"  "mother"  "brother" "sister"
x2
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
x3
## [1] "gold"  "red"   "green" "blue"
x4
## [1] 2 1 1 2
camera
##    member   brand color amount
## 1  father   Leica  gold      2
## 2  mother  Pentax   red      1
## 3 brother Olympus green      1
## 4  sister   Nikon  blue      2

資料框 – 特徵

camera
##    member   brand color amount
## 1  father   Leica  gold      2
## 2  mother  Pentax   red      1
## 3 brother Olympus green      1
## 4  sister   Nikon  blue      2
class(camera)
## [1] "data.frame"
names(camera)
## [1] "member" "brand"  "color"  "amount"
colnames(camera)        # column names
## [1] "member" "brand"  "color"  "amount"
rownames(camera)        # row names
## [1] "1" "2" "3" "4"

資料框 – 內容

camera
##    member   brand color amount
## 1  father   Leica  gold      2
## 2  mother  Pentax   red      1
## 3 brother Olympus green      1
## 4  sister   Nikon  blue      2
camera$brand
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
camera[, 2]
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"
camera[, "brand"]
## [1] "Leica"   "Pentax"  "Olympus" "Nikon"

資料框 – 加入一行數據

x5 <- c(8, 3, 2, 2)

camera$cost <- x5

camera
##    member   brand color amount cost
## 1  father   Leica  gold      2    8
## 2  mother  Pentax   red      1    3
## 3 brother Olympus green      1    2
## 4  sister   Nikon  blue      2    2

資料框 – 改變名稱

test <- camera

colnames(test)[c(4, 5)] <- c("number", "money")

test
##    member   brand color number money
## 1  father   Leica  gold      2     8
## 2  mother  Pentax   red      1     3
## 3 brother Olympus green      1     2
## 4  sister   Nikon  blue      2     2

資料框 – 捉取某一欄位的數據

品牌 (brand) 為 Leica 的資料

camera[ camera$brand == "Leica", ]
##   member brand color amount cost
## 1 father Leica  gold      2    8
subset( camera, brand == "Leica" )
##   member brand color amount cost
## 1 father Leica  gold      2    8

品牌 (brand) 為 Leica 或 Nikon 的資料

camera[ camera$brand %in% c("Leica", "Nikon"),  ]
##   member brand color amount cost
## 1 father Leica  gold      2    8
## 4 sister Nikon  blue      2    2
subset( camera, brand %in% c("Leica", "Nikon") )
##   member brand color amount cost
## 1 father Leica  gold      2    8
## 4 sister Nikon  blue      2    2

價格 (cost) 大於 2 的資料

camera[ camera$cost > 2, ]
##   member  brand color amount cost
## 1 father  Leica  gold      2    8
## 2 mother Pentax   red      1    3
subset( camera, cost > 2)
##   member  brand color amount cost
## 1 father  Leica  gold      2    8
## 2 mother Pentax   red      1    3

資料框 – 把矩陣轉為資料框

A <- matrix( c(1, -5, 4, 3, 6, -2 ), nrow = 2, ncol = 3 )

rownames( A )           # row names
## NULL
colnames( A )       # column names
## NULL
D <- as.data.frame( A )

names( D )
## [1] "V1" "V2" "V3"
colnames( D )
## [1] "V1" "V2" "V3"
rownames( D )
## [1] "1" "2"
D$V1
## [1]  1 -5
A
##      [,1] [,2] [,3]
## [1,]    1    4    6
## [2,]   -5    3   -2
D
##   V1 V2 V3
## 1  1  4  6
## 2 -5  3 -2

因子

x <- c("R", "G", "B", "R", "R", "B", "R", "G", "G")
class( x )
## [1] "character"
y <- factor( x )
class( y )
## [1] "factor"

這些等級可以用整數來表示

as.integer( y )
## [1] 3 2 1 3 3 1 3 2 2
levels( y )
## [1] "B" "G" "R"
levels( y )[2]
## [1] "G"
nlevels( y )
## [1] 3
levels( y )[ as.integer(y) ]
## [1] "R" "G" "B" "R" "R" "B" "R" "G" "G"

因子 – 5的等級,3個分量

gl( 5, 3 )          # factor levels up to 5 with repeats of 3
##  [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
## Levels: 1 2 3 4 5
gl( n = 5, k = 3 )
##  [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
## Levels: 1 2 3 4 5
class( gl( 5, 3 ) )
## [1] "factor"
gl( 5, 2, 13 )
##  [1] 1 1 2 2 3 3 4 4 5 5 1 1 2
## Levels: 1 2 3 4 5
gl( n = 5, k = 2, length = 13 )
##  [1] 1 1 2 2 3 3 4 4 5 5 1 1 2
## Levels: 1 2 3 4 5
is.factor( gl(5, 2, 13) )
## [1] TRUE

Including Plots

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.