# 13 多樣的資料結構：矩陣、資料框與更多

## 13.1 矩陣

my_mat <- matrix(1:6, nrow = 2)
my_mat
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
class(my_mat)
## [1] "matrix"

my_mat <- matrix(1:6, nrow = 2, byrow = TRUE)
my_mat
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6

my_mat <- matrix(1:6, nrow = 2)
my_mat
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
my_mat[2, 3] # 選出位於（2, 3）這個位置的 6
## [1] 6
my_mat[2, ] # 選出所有第二列（2nd row）的元素
## [1] 2 4 6
my_mat[, 3] # 選出所有第三欄（2nd column）的元素
## [1] 5 6

my_mat <- matrix(1:6, nrow = 2)
filter <- my_mat < 6 & my_mat > 1
my_mat[filter]
## [1] 2 3 4 5

my_mat <- matrix(c(1, 2, TRUE, FALSE, 3, 4), nrow = 2)
my_mat
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2    0    4
class(my_mat[, 2]) # 原本第二欄（2nd column）的輸入是兩個邏輯值
## [1] "numeric"

## 13.2 資料框

team_name <- c("Chicago Bulls", "Golden State Warriors")
wins <- c(72, 73)
losses <- c(10, 9)
is_champion <- c(TRUE, FALSE)
season <- c("1995-96", "2015-16")

great_nba_teams <- data.frame(team_name, wins, losses, is_champion, season)

team_name <- c("Chicago Bulls", "Golden State Warriors")
wins <- c(72, 73)
losses <- c(10, 9)
is_champion <- c(TRUE, FALSE)
season <- c("1995-96", "2015-16")
great_nba_teams <- data.frame(team_name, wins, losses, is_champion, season)
great_nba_teams[1, 1] # 選出第一個變數的第一個觀測值 "Chicago Bulls"
## [1] Chicago Bulls
## Levels: Chicago Bulls Golden State Warriors
great_nba_teams[1, ] # 選出第一個觀測值
##       team_name wins losses is_champion  season
## 1 Chicago Bulls   72     10        TRUE 1995-96
great_nba_teams[, 1] # 選出第一個變數
## [1] Chicago Bulls         Golden State Warriors
## Levels: Chicago Bulls Golden State Warriors

team_name <- c("Chicago Bulls", "Golden State Warriors")
wins <- c(72, 73)
losses <- c(10, 9)
is_champion <- c(TRUE, FALSE)
season <- c("1995-96", "2015-16")

great_nba_teams <- data.frame(team_name, wins, losses, is_champion, season, stringsAsFactors = FALSE)
great_nba_teams[, 1] # 選出第一個變數
## [1] "Chicago Bulls"         "Golden State Warriors"
great_nba_teams[, 5] # 選出第五個變數
## [1] "1995-96" "2015-16"

team_name <- c("Chicago Bulls", "Golden State Warriors")
wins <- c(72, 73)
losses <- c(10, 9)
is_champion <- c(TRUE, FALSE)
season <- c("1995-96", "2015-16")

great_nba_teams <- data.frame(team_name, wins, losses, is_champion, season)
great_nba_teams[, 1] <- as.character(great_nba_teams[, 1])
great_nba_teams[, 5] <- as.character(great_nba_teams[, 5])
great_nba_teams[, 1] # 選出第一個變數
## [1] "Chicago Bulls"         "Golden State Warriors"
great_nba_teams[, 5] # 選出第五個變數
## [1] "1995-96" "2015-16"

## [1] "Chicago Bulls"         "Golden State Warriors"
great_nba_teams[, "season"]
## [1] "1995-96" "2015-16"

team_name <- c("Chicago Bulls", "Golden State Warriors")
wins <- c(72, 73)
losses <- c(10, 9)
is_champion <- c(TRUE, FALSE)
season <- c("1995-96", "2015-16")

great_nba_teams <- data.frame(team_name, wins, losses, is_champion, season, stringsAsFactors = FALSE)
filter <- great_nba_teams$is_champion == TRUE great_nba_teams[filter, ] # 注意這個逗號 ## team_name wins losses is_champion season ## 1 Chicago Bulls 72 10 TRUE 1995-96 請讀者在這裡一定要注意我們的篩選是針對觀測值（水平方向），所以要將判斷運算子判斷後的結果（filter）放在逗號前面，逗號後面留空（我們沒有要選擇變數。） 資料框不像矩陣僅能包含一種變數類型，您可以發現在我們的 great_nba_teams 資料框中，有文字（team_nameseason）、整數（winslosses）與邏輯值（is_champion），我們可以用 str() 函數觀察： team_name <- c("Chicago Bulls", "Golden State Warriors") wins <- c(72, 73) losses <- c(10, 9) is_champion <- c(TRUE, FALSE) season <- c("1995-96", "2015-16") great_nba_teams <- data.frame(team_name, wins, losses, is_champion, season, stringsAsFactors = FALSE) str(great_nba_teams) ## 'data.frame': 2 obs. of 5 variables: ##$ team_name  : chr  "Chicago Bulls" "Golden State Warriors"
##  $wins : num 72 73 ##$ losses     : num  10 9
##  $is_champion: logi TRUE FALSE ##$ season     : chr  "1995-96" "2015-16"