認識向量

Doug: Remember, there’s no “I” in team. Chandler: Yes, but there’s two in martini, so everybody back to my office!

Friends

基本資料單位:向量

我們都玩過像是樂高、積木、模型或拼圖等遊戲,在這些遊戲中透過堆疊組裝不同外型、不同功能的零件最終發展為成品。在撰寫 R 語言同樣需要利用基本資料單位(building block)建造出軟體應用、分析系統,而 R 語言的基本資料單位稱作向量(vector),這一點與大多數廣泛用途程式語言(general-purposed programming language)有著極為巨大的差異。

簡單來說若是在 Console 印出 "Hello world!" ,對 R 語言來說並不是以所謂的**純量(scalar)**來看待這個文字,而是將其視作長度為 1 的文字向量。

hello_world <- "Hello world"
hello_world
# Check if hello_world is a vector or not
is.vector(hello_world)
# Check the length of hello_world
length(hello_world)
1
2
3
4
5
6
## > hello_world <- "Hello world"
## > hello_world
## [1] "Hello world"
## > # Check if hello_world is a vector or not
## > is.vector(hello_world)
## [1] TRUE
## > # Check the length of hello_world
## > length(hello_world)
## [1] 1
1
2
3
4
5
6
7
8
9

這樣以向量作為基本資料單位的設定體現了 R 語言作為一個以資料分析為主軸的程式語言理念,對她而言,任何運算都是元素級別(element-wise)和函數以映射方式應用到每個元素之上。也如同樂高積木不同元件一般,R 語言中的向量也有不同的類型,包含有:數值向量(numeric)、整數向量(integer)、文字向量(character)、邏輯值向量(logical)、日期向量(Date)與日期時間向量(POSIXct)。

如何創建向量

創建長度為 1 的向量非常簡單,只需要用 <- 將不同類型的資料指派給物件名稱即可,使用者不需要自己去猜測向量類型,而是使用一個函數 class() 讓 R 語言告訴我們該物件是什麼類型的向量。

num_vector <- 87
class(num_vector)
int_vector <- 87L
class(int_vector)
logi_vector <- TRUE
class(logi_vector)
chr_vector <- "Learn R the easy way"
class(chr_vector)
logi_vector <- FALSE
class(logi_vector)
date_vector <- Sys.Date()
class(date_vector)
datetime_vector <- Sys.time()
class(datetime_vector)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
## > num_vector <- 87
## > class(num_vector)
## [1] "numeric"
## > int_vector <- 87L
## > class(int_vector)
## [1] "integer"
## > logi_vector <- TRUE
## > class(logi_vector)
## [1] "logical"
## > chr_vector <- "Learn R the easy way"
## > class(chr_vector)
## [1] "character"
## > logi_vector <- FALSE
## > class(logi_vector)
## [1] "logical"
## > date_vector <- Sys.Date()
## > class(date_vector)
## [1] "Date"
## > datetime_vector <- Sys.time()
## > class(datetime_vector)
## [1] "POSIXct" "POSIXt" 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

如果想要創建長度大於 1 的向量,最常使用的方法是利用 c() 函數(combine 的縮寫),在 c() 函數中可以將多筆資料以逗號分隔存入一個物件之中。

nums <- c(87, 78)
nums
ints <- c(87L, 78L)
ints
chars <- c("Learn", "R", "the", "easy", "way")
chars
logicals <- c(TRUE, FALSE)
logicals
dates <- c(Sys.Date(), Sys.Date() + 1)
dates
datetimes <- c(Sys.time(), Sys.time() + 86400)
datetimes
1
2
3
4
5
6
7
8
9
10
11
12
## > nums <- c(87, 78)
## > nums
## [1] 87 78
## > ints <- c(87L, 78L)
## > ints
## [1] 87 78
## > chars <- c("Learn", "R", "the", "easy", "way")
## > chars
## [1] "Learn" "R"     "the"   "easy"  "way"  
## > logicals <- c(TRUE, FALSE)
## > logicals
## [1]  TRUE FALSE
## > dates <- c(Sys.Date(), Sys.Date() + 1)
## > dates
## [1] "2018-11-13" "2018-11-14"
## > datetimes <- c(Sys.time(), Sys.time() + 86400)
## > datetimes
## [1] "2018-11-13 13:38:54 CST" "2018-11-14 13:38:54 CST"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

rep() 函數可以生成包含重複資料的向量,其中 times 參數可以指定向量中要有幾個重複值:

rep(7, times = 8)
rep("R", times = 5)
rep(TRUE, times = 5)
rep(Sys.Date(), times = 2)
rep(Sys.time(), times = 2)
1
2
3
4
5
## > rep(7, times = 8)
## [1] 7 7 7 7 7 7 7 7
## > rep("R", times = 5)
## [1] "R" "R" "R" "R" "R"
## > rep(TRUE, times = 5)
## [1] TRUE TRUE TRUE TRUE TRUE
## > rep(Sys.Date(), times = 2)
## [1] "2018-11-13" "2018-11-13"
## > rep(Sys.time(), times = 2)
## [1] "2018-11-13 13:41:09 CST" "2018-11-13 13:41:09 CST"
1
2
3
4
5
6
7
8
9
10

數值向量

建立長度大於 1 的數值向量除了如前述一般使用 c() 函數或者 rep() 函數,亦可以呼叫 seq() 函數或 : 符號建立具有規則性的數值向量。 seq() 函數可以生成等差數列,其中 from 參數指定數列的起始值,to 參數指定數列的終止值,by 參數指定數值的間距。

seq(from = 7, to = 77, by = 7)
seq(from = 1, to = 10, by = 1)
1
2
## > seq(from = 7, to = 77, by = 7)
##  [1]  7 14 21 28 35 42 49 56 63 70 77
## > seq(from = 1, to = 10, by = 1)
##  [1]  1  2  3  4  5  6  7  8  9 10
1
2
3
4

如果單純是要生成數值間距為 1 的數值向量,用 : 符號更快捷。

1:10
11:20
1
2
## > 1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
## > 11:20
##  [1] 11 12 13 14 15 16 17 18 19 20
1
2
3
4

不論輸入數字帶有或不帶有小數位數,R 語言預設都以數值向量(numeric)儲存。

my_num <- 8.7
class(my_num)
my_num <- 87.0
class(my_num)
my_num <- 87
class(my_num)
1
2
3
4
5
6
## > my_num <- 8.7
## > class(my_num)
## [1] "numeric"
## > my_num <- 87.0
## > class(my_num)
## [1] "numeric"
## > my_num <- 87
## > class(my_num)
## [1] "numeric"
1
2
3
4
5
6
7
8
9

整數向量

當我們輸入整數並加入大寫英文字母 L 作註記,R 語言就會改儲存為整數向量(integer)。若是在整數向量中帶有不必要的小數位數零,R 語言會回傳警示訊息但依舊會儲存為整數向量;但若在帶有必要小數位數的數字後加上大寫英文字母 L 註記則 R 語言將回傳警示訊息,並且忽略整數向量的註記改以數值向量儲存。

my_int <- 87L
class(my_int)
my_int <- 87.0L
class(my_int)
my_int <- 87.87L
class(my_int)
1
2
3
4
5
6
## > my_int <- 87L
## > class(my_int)
## [1] "integer"
## > my_int <- 87.0L
## Warning message:
## integer literal 87.0L contains unnecessary decimal point 
## > class(my_int)
## [1] "integer"
## > my_int <- 87.87L
## Warning message:
## integer literal 87.87L contains decimal; using numeric value 
## > class(my_int)
## [1] "numeric"
1
2
3
4
5
6
7
8
9
10
11
12
13

數值與整數向量可以進行元素級別的數學運算,常用的數學運算子有七種:

  • +-*/ :加減乘除
  • ^** :次方
  • %/% :回傳商數
  • %% :回傳餘數
first_nums <- 11:13
second_nums <- 1:3
first_nums + second_nums
first_nums - second_nums
first_nums * second_nums
first_nums / second_nums
first_nums**second_nums
first_nums %/% second_nums
first_nums %% second_nums
1
2
3
4
5
6
7
8
9
## > first_nums <- 11:13
## > second_nums <- 1:3
## > first_nums + second_nums
## [1] 12 14 16
## > first_nums - second_nums
## [1] 10 10 10
## > first_nums * second_nums
## [1] 11 24 39
## > first_nums / second_nums
## [1] 11.000000  6.000000  4.333333
## > first_nums**second_nums
## [1]   11  144 2197
## > first_nums %/% second_nums
## [1] 11  6  4
## > first_nums %% second_nums
## [1] 0 0 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

文字向量

在 R 語言中我們可以使用成雙的單引號 '' 或成對的雙引號 "" 來建立文字向量(character),多數的時候使用單引號或者雙引號不會有任何分別。

hellos <- c("Hello world", 'Hello R')
hellos
class(hellos)
1
2
3
## > hellos <- c("Hello world", 'Hello R')
## > hellos
## [1] "Hello world" "Hello R"    
## > class(hellos)
## [1] "character"
1
2
3
4
5

但是在部分情境中,使用成雙的單引號或成對的雙引號標註文字向量是有差別的,像是在英文句子中經常出現的非成雙單引號 '(apostrophe)以及用作嘲諷強調的空氣雙引號(air-quotes),當文字向量中的內容有出現這些元件時如果沒有特別關注,宣告的當下就會產生錯誤。

# Error: unexpected symbol
mcd <- 'I'm lovin it'
shaq <- 'Shaquille O'Neal'
ross_said <- "Let's put aside the fact that you "accidentally" pick up my grandmother's ring."
1
2
3
4
## > mcd <- 'I'm lovin it'
## Error: unexpected symbol in "mcd <- 'I'm"
## > shaq <- 'Shaquille O'Neal'
## Error: unexpected symbol in "shaq <- 'Shaquille O'Neal"
## > ross_said <- "Let's put aside the fact that you "accidentally" pick up my grandmother's ring."
## Error: unexpected symbol in "ross_said <- "Let's put aside the fact that you "accidentally"
1
2
3
4
5
6

這時候我們可以使用跳脫字元反斜線 \ 來完成宣告或者使用不同樣式的引號。

mcd <- 'I\'m lovin it' # mcd <- "I'm lovin it" 
shaq <- 'Shaquille O\'Neal' # shaq <- "Shaquille O'Neal"
ross_said <- "Let's put aside the fact that you \"accidentally\" pick up my grandmother's ring." # ross_said <- 'Let\'s put aside the fact that you "accidentally" pick up my grandmother\'s ring.'
writeLines(mcd)
writeLines(shaq)
writeLines(ross_said)
1
2
3
4
5
6
## > mcd <- 'I\'m lovin it' # mcd <- "I'm lovin it" 
## > shaq <- 'Shaquille O\'Neal' # shaq <- "Shaquille O'Neal"
## > ross_said <- "Let's put aside the fact that you \"accidentally\" pick up my grandmother's ring." # ross_said <- 'Let\'s put aside the fact that you "accidentally" pick up my grandmother\'s ring.'
## > writeLines(mcd)
## I'm lovin it
## > writeLines(shaq)
## Shaquille O'Neal
## > writeLines(ross_said)
## Let's put aside the fact that you "accidentally" pick up my grandmother's ring.
1
2
3
4
5
6
7
8
9

運用文字向量時常會使用到的技巧是以特定格式印出(print with format),具體來說是在文字向量中嵌入已經宣告好的向量,以 sprintf() 函數作為實踐的管道,以 %s 作文字向量或者以 %f 做數值向量的等不同類型的嵌入。

team <- "Chicago Bulls"
season <- "1995-1996"
wins <- 72
losses <- 10
sprintf("%s recorded a %.0f-win, %.0f-loss season in %s", team, wins, losses, season)
1
2
3
4
5
## > team <- "Chicago Bulls"
## > season <- "1995-1996"
## > wins <- 72
## > losses <- 10
## > sprintf("%s recorded a %.0f-win, %.0f-loss season in %s", team, wins, losses, season)
## [1] "Chicago Bulls recorded a 72-win, 10-loss season in 1995-1996"
1
2
3
4
5
6

邏輯值向量

當我們進行判斷條件或者資料篩選的時候會需要仰賴邏輯值向量(logical),邏輯值向量只有 TRUEFALSE 這兩個值,亦可以簡寫為 TF

class(TRUE)
class(FALSE)
class(T)
class(F)
1
2
3
4
## > class(TRUE)
## [1] "logical"
## > class(FALSE)
## [1] "logical"
## > class(T)
## [1] "logical"
## > class(F)
## [1] "logical"
1
2
3
4
5
6
7
8

這裡特別提醒一個觀念,R 語言(或者絕大多數的程式語言)對於英文的大小寫是敏感的(case-sensitive),像是 TRUE 會被識別為邏輯值向量,但是 True 或者 true 則會被視作物件名稱。

class(TRUE)
class(True)
class(true)
1
2
3
## > class(TRUE)
## [1] "logical"
## > class(True)
## Error: object 'True' not found
## > class(true)
## Error: object 'true' not found
1
2
3
4
5
6

除了直接輸入邏輯值向量,我們也可以透過判斷條件得到邏輯值向量,常用的邏輯運算符號有:

  • ==!=:等於以及不等於
  • >>=<<=:大於、大於等於、小於以及小於等於
  • %in%:包含於
  • !:非
8 > 7 # 判斷 8 是否大於 7
8 < 7 # 判斷 8 是否小於 7
8 >= 7 # 判斷 8 是否大於等於 7
8 <= 7 # 判斷 8 是否小於等於 7
8 == 7 # 判斷 8 是否等於 7
8 != 7 # 判斷 8 是否不等於 7
7 %in% c(8, 7) # 判斷 7 是否包含於一個數值向量之中
!(8 > 7) # 反轉 8 是否大於 7 的判斷
1
2
3
4
5
6
7
8
## > 8 > 7 # 判斷 8 是否大於 7
## [1] TRUE
## > 8 < 7 # 判斷 8 是否小於 7
## [1] FALSE
## > 8 >= 7 # 判斷 8 是否大於等於 7
## [1] TRUE
## > 8 <= 7 # 判斷 8 是否小於等於 7
## [1] FALSE
## > 8 == 7 # 判斷 8 是否等於 7
## [1] FALSE
## > 8 != 7 # 判斷 8 是否不等於 7
## [1] TRUE
## > 7 %in% c(8, 7) # 判斷 7 是否包含於一個數值向量之中
## [1] TRUE
## > !(8 > 7) # 反轉 8 是否大於 7 的判斷
## [1] FALSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

在 R 語言中, TRUE 跟數值 1 相等; FALSE 跟數值 0 相等。如果在數值向量運算中納入了邏輯值向量不會產生任何問題。

numerics <- c(0, 1)
logicals <- c(FALSE, TRUE)
numerics == logicals
1
2
3
## > numerics <- c(0, 1)
## > logicals <- c(FALSE, TRUE)
## > numerics == logicals
## [1] TRUE TRUE
1
2
3
4

有關於邏輯值向量在判斷條件或者資料篩選的應用,將在後續的章節中探討。

日期向量

在 R 語言中日期向量(Date)的外觀看起來跟文字向量並沒有什麼差別,但是當我們一但將它們放入 class() 函數中檢驗,就會發現並不是文字,接下來使用的 Sys.Date() 函數作用是不需要參數就會輸出所使用電腦的系統日期向量。

sys_date <- Sys.Date() # 系統日期向量
sys_date # 看起來跟文字相同
class(sys_date) # 但並不是文字向量
1
2
3
## > sys_date <- Sys.Date() # 系統日期向量
## > sys_date # 看起來跟文字相同
## [1] "2018-11-13"
## > class(sys_date) # 但並不是文字向量
## [1] "Date"
1
2
3
4
5

文字向量與日期向量兩者最大的分野,就在於日期向量可以被轉換為數值向量,而文字向量不行:

sys_date <- Sys.Date()
sys_date_char <- as.character(sys_date) # 創造一個文字類型
as.numeric(sys_date)
as.numeric(sys_date_char)
1
2
3
4
## > sys_date <- Sys.Date()
## > sys_date_char <- as.character(sys_date) # 創造一個文字類型
## > as.numeric(sys_date)
## [1] 17848
## > as.numeric(sys_date_char)
## [1] NA
## Warning message:
## NAs introduced by coercion
1
2
3
4
5
6
7
8

這裡我們使用 as.character()as.numeric() 函數是在 R 語言中用來進行向量類型轉換的函數,我們可以清楚觀察到文字向量在轉換數值向量時失敗,以致於產生一個遺失值(Not Available,NA)。

那麼日期類型的系統日期轉換的整數是有什麼根據嗎?答案是有的,R 語言預設以西元 1970 年 1 月 1 日作為 0,在這一天以後的每天都 +1 來記錄,而這一天以前的每天都 -1 來記錄。

date_of_origin <- as.Date("1970-01-01")
as.numeric(date_of_origin)
as.numeric(date_of_origin + 1)
as.numeric(date_of_origin - 1)
date_of_origin
date_of_origin + 1
date_of_origin - 1
1
2
3
4
5
6
7
## > date_of_origin <- as.Date("1970-01-01")
## > as.numeric(date_of_origin)
## [1] 0
## > as.numeric(date_of_origin + 1)
## [1] 1
## > as.numeric(date_of_origin - 1)
## [1] -1
## > date_of_origin
## [1] "1970-01-01"
## > date_of_origin + 1
## [1] "1970-01-02"
## > date_of_origin - 1
## [1] "1969-12-31"
1
2
3
4
5
6
7
8
9
10
11
12
13

也正因為具備這樣的特性,日期向量可以進行數值運算,而文字類型不行:

sys_date <- Sys.Date()
sys_date_char <- as.character(sys_date) # 創造一個文字類型
sys_date - 1 # 昨天的日期
sys_date_char - 1
1
2
3
4
## > sys_date <- Sys.Date()
## > sys_date_char <- as.character(sys_date) # 創造一個文字類型
## > sys_date - 1 # 昨天的日期
## [1] "2018-11-11"
## > sys_date_char - 1
## Error in sys_date_char - 1 : non-numeric argument to binary operator
1
2
3
4
5
6

日期時間向量

在 R 語言中被定義為日期時間向量(POSIXct)的外觀看起來跟文字同樣也沒有什麼差別,但是我們一但將它們放入 class() 函數中檢驗,就會發現它並不是文字,我們接下來要使用的 Sys.time() 函數作用是不需要參數就會輸出所使用電腦的系統日期向量。

sys_time <- Sys.time() # 系統日期時間
sys_time # 看起來跟文字相同
class(sys_time)
1
2
3
## > sys_time <- Sys.time() # 系統日期時間
## > sys_time # 看起來跟文字相同
## [1] "2018-11-13 14:07:50 CST"
## > class(sys_time)
## [1] "POSIXct" "POSIXt"
1
2
3
4
5

與日期向量相同,日期時間向量亦是可以被轉換為數值向量的:

sys_time <- Sys.time()
as.numeric(sys_time)
1
2
## > sys_time <- Sys.time()
## > as.numeric(sys_time)
## [1] 1542089312
1
2
3

R 語言預設以西元 1970 年 1 月 1 日格林尼治標準時間(Greenwich Mean Time,GMT)00 時 00 分 00 秒作為 0,在這個時間點以後的每秒都 +1 來記錄,這個時間點以前的每秒都 -1 來記錄。

datetime_of_origin <- as.POSIXct("1970-01-01 00:00:00", tz = "GMT")
as.numeric(datetime_of_origin)
as.numeric(datetime_of_origin + 1)
as.integer(datetime_of_origin - 1)
datetime_of_origin
datetime_of_origin + 1
datetime_of_origin - 1
1
2
3
4
5
6
7
## > datetime_of_origin <- as.POSIXct("1970-01-01 00:00:00", tz = "GMT")
## > as.numeric(datetime_of_origin)
## [1] 0
## > as.numeric(datetime_of_origin + 1)
## [1] 1
## > as.integer(datetime_of_origin - 1)
## [1] -1
## > datetime_of_origin
## [1] "1970-01-01 GMT"
## > datetime_of_origin + 1
## [1] "1970-01-01 00:00:01 GMT"
## > datetime_of_origin - 1
## [1] "1969-12-31 23:59:59 GMT"
1
2
3
4
5
6
7
8
9
10
11
12
13

我們這裡所使用的參數 tz = "GMT" 是指定時區,假如您的電腦和我一樣時區是設在中原標準時間(Chungyuan Standard Time,CST),早格林尼治標準時間八個小時(GMT + 8),則基準日期時間會是西元 1970 年 1 月 1 日 08 時 00 分 00 秒。

datetime_of_origin_cst <- as.POSIXct("1970-01-01 08:00:00")
as.numeric(datetime_of_origin_cst)
1
2
## > datetime_of_origin_cst <- as.POSIXct("1970-01-01 08:00:00")
## > as.numeric(datetime_of_origin_cst)
## [1] 0
1
2
3

想知道 tz 參數如何指定,可以使用 OlsonNames() 函數檢視 R 語言認識的時區名稱與數量。

length(OlsonNames())
OlsonNames()
1
2
## > length(OlsonNames())
## [1] 592
## > OlsonNames()
##   [1] "Africa/Abidjan"                   "Africa/Accra"                    
##   [3] "Africa/Addis_Ababa"               "Africa/Algiers"                  
##   [5] "Africa/Asmara"                    "Africa/Asmera"                   
##   [7] "Africa/Bamako"                    "Africa/Bangui"                   
##   [9] "Africa/Banjul"                    "Africa/Bissau"                   
##  [11] "Africa/Blantyre"                  "Africa/Brazzaville"              
##  [13] "Africa/Bujumbura"                 "Africa/Cairo"                    
##  [15] "Africa/Casablanca"                "Africa/Ceuta"                    
##  [17] "Africa/Conakry"                   "Africa/Dakar"                    
##  [19] "Africa/Dar_es_Salaam"             "Africa/Djibouti"                 
##  [21] "Africa/Douala"                    "Africa/El_Aaiun"                 
##  [23] "Africa/Freetown"                  "Africa/Gaborone"                 
##  [25] "Africa/Harare"                    "Africa/Johannesburg"             
##  [27] "Africa/Juba"                      "Africa/Kampala"                  
##  [29] "Africa/Khartoum"                  "Africa/Kigali"                   
##  [31] "Africa/Kinshasa"                  "Africa/Lagos"                    
##  [33] "Africa/Libreville"                "Africa/Lome"                     
##  [35] "Africa/Luanda"                    "Africa/Lubumbashi"               
##  [37] "Africa/Lusaka"                    "Africa/Malabo"                   
##  [39] "Africa/Maputo"                    "Africa/Maseru"                   
##  [41] "Africa/Mbabane"                   "Africa/Mogadishu"                
##  [43] "Africa/Monrovia"                  "Africa/Nairobi"                  
##  [45] "Africa/Ndjamena"                  "Africa/Niamey"                   
##  [47] "Africa/Nouakchott"                "Africa/Ouagadougou"              
##  [49] "Africa/Porto-Novo"                "Africa/Sao_Tome"                 
##  [51] "Africa/Timbuktu"                  "Africa/Tripoli"                  
##  [53] "Africa/Tunis"                     "Africa/Windhoek"                 
##  [55] "America/Adak"                     "America/Anchorage"               
##  [57] "America/Anguilla"                 "America/Antigua"                 
##  [59] "America/Araguaina"                "America/Argentina/Buenos_Aires"  
##  [61] "America/Argentina/Catamarca"      "America/Argentina/ComodRivadavia"
##  [63] "America/Argentina/Cordoba"        "America/Argentina/Jujuy"         
##  [65] "America/Argentina/La_Rioja"       "America/Argentina/Mendoza"       
##  [67] "America/Argentina/Rio_Gallegos"   "America/Argentina/Salta"         
##  [69] "America/Argentina/San_Juan"       "America/Argentina/San_Luis"      
##  [71] "America/Argentina/Tucuman"        "America/Argentina/Ushuaia"       
##  [73] "America/Aruba"                    "America/Asuncion"                
##  [75] "America/Atikokan"                 "America/Atka"                    
##  [77] "America/Bahia"                    "America/Bahia_Banderas"          
##  [79] "America/Barbados"                 "America/Belem"                   
##  [81] "America/Belize"                   "America/Blanc-Sablon"            
##  [83] "America/Boa_Vista"                "America/Bogota"                  
##  [85] "America/Boise"                    "America/Buenos_Aires"            
##  [87] "America/Cambridge_Bay"            "America/Campo_Grande"            
##  [89] "America/Cancun"                   "America/Caracas"                 
##  [91] "America/Catamarca"                "America/Cayenne"                 
##  [93] "America/Cayman"                   "America/Chicago"                 
##  [95] "America/Chihuahua"                "America/Coral_Harbour"           
##  [97] "America/Cordoba"                  "America/Costa_Rica"              
##  [99] "America/Creston"                  "America/Cuiaba"                  
## [101] "America/Curacao"                  "America/Danmarkshavn"            
## [103] "America/Dawson"                   "America/Dawson_Creek"            
## [105] "America/Denver"                   "America/Detroit"                 
## [107] "America/Dominica"                 "America/Edmonton"                
## [109] "America/Eirunepe"                 "America/El_Salvador"             
## [111] "America/Ensenada"                 "America/Fort_Nelson"             
## [113] "America/Fort_Wayne"               "America/Fortaleza"               
## [115] "America/Glace_Bay"                "America/Godthab"                 
## [117] "America/Goose_Bay"                "America/Grand_Turk"              
## [119] "America/Grenada"                  "America/Guadeloupe"              
## [121] "America/Guatemala"                "America/Guayaquil"               
## [123] "America/Guyana"                   "America/Halifax"                 
## [125] "America/Havana"                   "America/Hermosillo"              
## [127] "America/Indiana/Indianapolis"     "America/Indiana/Knox"            
## [129] "America/Indiana/Marengo"          "America/Indiana/Petersburg"      
## [131] "America/Indiana/Tell_City"        "America/Indiana/Vevay"           
## [133] "America/Indiana/Vincennes"        "America/Indiana/Winamac"         
## [135] "America/Indianapolis"             "America/Inuvik"                  
## [137] "America/Iqaluit"                  "America/Jamaica"                 
## [139] "America/Jujuy"                    "America/Juneau"                  
## [141] "America/Kentucky/Louisville"      "America/Kentucky/Monticello"     
## [143] "America/Knox_IN"                  "America/Kralendijk"              
## [145] "America/La_Paz"                   "America/Lima"                    
## [147] "America/Los_Angeles"              "America/Louisville"              
## [149] "America/Lower_Princes"            "America/Maceio"                  
## [151] "America/Managua"                  "America/Manaus"                  
## [153] "America/Marigot"                  "America/Martinique"              
## [155] "America/Matamoros"                "America/Mazatlan"                
## [157] "America/Mendoza"                  "America/Menominee"               
## [159] "America/Merida"                   "America/Metlakatla"              
## [161] "America/Mexico_City"              "America/Miquelon"                
## [163] "America/Moncton"                  "America/Monterrey"               
## [165] "America/Montevideo"               "America/Montreal"                
## [167] "America/Montserrat"               "America/Nassau"                  
## [169] "America/New_York"                 "America/Nipigon"                 
## [171] "America/Nome"                     "America/Noronha"                 
## [173] "America/North_Dakota/Beulah"      "America/North_Dakota/Center"     
## [175] "America/North_Dakota/New_Salem"   "America/Ojinaga"                 
## [177] "America/Panama"                   "America/Pangnirtung"             
## [179] "America/Paramaribo"               "America/Phoenix"                 
## [181] "America/Port_of_Spain"            "America/Port-au-Prince"          
## [183] "America/Porto_Acre"               "America/Porto_Velho"             
## [185] "America/Puerto_Rico"              "America/Punta_Arenas"            
## [187] "America/Rainy_River"              "America/Rankin_Inlet"            
## [189] "America/Recife"                   "America/Regina"                  
## [191] "America/Resolute"                 "America/Rio_Branco"              
## [193] "America/Rosario"                  "America/Santa_Isabel"            
## [195] "America/Santarem"                 "America/Santiago"                
## [197] "America/Santo_Domingo"            "America/Sao_Paulo"               
## [199] "America/Scoresbysund"             "America/Shiprock"                
## [201] "America/Sitka"                    "America/St_Barthelemy"           
## [203] "America/St_Johns"                 "America/St_Kitts"                
## [205] "America/St_Lucia"                 "America/St_Thomas"               
## [207] "America/St_Vincent"               "America/Swift_Current"           
## [209] "America/Tegucigalpa"              "America/Thule"                   
## [211] "America/Thunder_Bay"              "America/Tijuana"                 
## [213] "America/Toronto"                  "America/Tortola"                 
## [215] "America/Vancouver"                "America/Virgin"                  
## [217] "America/Whitehorse"               "America/Winnipeg"                
## [219] "America/Yakutat"                  "America/Yellowknife"             
## [221] "Antarctica/Casey"                 "Antarctica/Davis"                
## [223] "Antarctica/DumontDUrville"        "Antarctica/Macquarie"            
## [225] "Antarctica/Mawson"                "Antarctica/McMurdo"              
## [227] "Antarctica/Palmer"                "Antarctica/Rothera"              
## [229] "Antarctica/South_Pole"            "Antarctica/Syowa"                
## [231] "Antarctica/Troll"                 "Antarctica/Vostok"               
## [233] "Arctic/Longyearbyen"              "Asia/Aden"                       
## [235] "Asia/Almaty"                      "Asia/Amman"                      
## [237] "Asia/Anadyr"                      "Asia/Aqtau"                      
## [239] "Asia/Aqtobe"                      "Asia/Ashgabat"                   
## [241] "Asia/Ashkhabad"                   "Asia/Atyrau"                     
## [243] "Asia/Baghdad"                     "Asia/Bahrain"                    
## [245] "Asia/Baku"                        "Asia/Bangkok"                    
## [247] "Asia/Barnaul"                     "Asia/Beirut"                     
## [249] "Asia/Bishkek"                     "Asia/Brunei"                     
## [251] "Asia/Calcutta"                    "Asia/Chita"                      
## [253] "Asia/Choibalsan"                  "Asia/Chongqing"                  
## [255] "Asia/Chungking"                   "Asia/Colombo"                    
## [257] "Asia/Dacca"                       "Asia/Damascus"                   
## [259] "Asia/Dhaka"                       "Asia/Dili"                       
## [261] "Asia/Dubai"                       "Asia/Dushanbe"                   
## [263] "Asia/Famagusta"                   "Asia/Gaza"                       
## [265] "Asia/Harbin"                      "Asia/Hebron"                     
## [267] "Asia/Ho_Chi_Minh"                 "Asia/Hong_Kong"                  
## [269] "Asia/Hovd"                        "Asia/Irkutsk"                    
## [271] "Asia/Istanbul"                    "Asia/Jakarta"                    
## [273] "Asia/Jayapura"                    "Asia/Jerusalem"                  
## [275] "Asia/Kabul"                       "Asia/Kamchatka"                  
## [277] "Asia/Karachi"                     "Asia/Kashgar"                    
## [279] "Asia/Kathmandu"                   "Asia/Katmandu"                   
## [281] "Asia/Khandyga"                    "Asia/Kolkata"                    
## [283] "Asia/Krasnoyarsk"                 "Asia/Kuala_Lumpur"               
## [285] "Asia/Kuching"                     "Asia/Kuwait"                     
## [287] "Asia/Macao"                       "Asia/Macau"                      
## [289] "Asia/Magadan"                     "Asia/Makassar"                   
## [291] "Asia/Manila"                      "Asia/Muscat"                     
## [293] "Asia/Nicosia"                     "Asia/Novokuznetsk"               
## [295] "Asia/Novosibirsk"                 "Asia/Omsk"                       
## [297] "Asia/Oral"                        "Asia/Phnom_Penh"                 
## [299] "Asia/Pontianak"                   "Asia/Pyongyang"                  
## [301] "Asia/Qatar"                       "Asia/Qyzylorda"                  
## [303] "Asia/Rangoon"                     "Asia/Riyadh"                     
## [305] "Asia/Saigon"                      "Asia/Sakhalin"                   
## [307] "Asia/Samarkand"                   "Asia/Seoul"                      
## [309] "Asia/Shanghai"                    "Asia/Singapore"                  
## [311] "Asia/Srednekolymsk"               "Asia/Taipei"                     
## [313] "Asia/Tashkent"                    "Asia/Tbilisi"                    
## [315] "Asia/Tehran"                      "Asia/Tel_Aviv"                   
## [317] "Asia/Thimbu"                      "Asia/Thimphu"                    
## [319] "Asia/Tokyo"                       "Asia/Tomsk"                      
## [321] "Asia/Ujung_Pandang"               "Asia/Ulaanbaatar"                
## [323] "Asia/Ulan_Bator"                  "Asia/Urumqi"                     
## [325] "Asia/Ust-Nera"                    "Asia/Vientiane"                  
## [327] "Asia/Vladivostok"                 "Asia/Yakutsk"                    
## [329] "Asia/Yangon"                      "Asia/Yekaterinburg"              
## [331] "Asia/Yerevan"                     "Atlantic/Azores"                 
## [333] "Atlantic/Bermuda"                 "Atlantic/Canary"                 
## [335] "Atlantic/Cape_Verde"              "Atlantic/Faeroe"                 
## [337] "Atlantic/Faroe"                   "Atlantic/Jan_Mayen"              
## [339] "Atlantic/Madeira"                 "Atlantic/Reykjavik"              
## [341] "Atlantic/South_Georgia"           "Atlantic/St_Helena"              
## [343] "Atlantic/Stanley"                 "Australia/ACT"                   
## [345] "Australia/Adelaide"               "Australia/Brisbane"              
## [347] "Australia/Broken_Hill"            "Australia/Canberra"              
## [349] "Australia/Currie"                 "Australia/Darwin"                
## [351] "Australia/Eucla"                  "Australia/Hobart"                
## [353] "Australia/LHI"                    "Australia/Lindeman"              
## [355] "Australia/Lord_Howe"              "Australia/Melbourne"             
## [357] "Australia/North"                  "Australia/NSW"                   
## [359] "Australia/Perth"                  "Australia/Queensland"            
## [361] "Australia/South"                  "Australia/Sydney"                
## [363] "Australia/Tasmania"               "Australia/Victoria"              
## [365] "Australia/West"                   "Australia/Yancowinna"            
## [367] "Brazil/Acre"                      "Brazil/DeNoronha"                
## [369] "Brazil/East"                      "Brazil/West"                     
## [371] "Canada/Atlantic"                  "Canada/Central"                  
## [373] "Canada/Eastern"                   "Canada/Mountain"                 
## [375] "Canada/Newfoundland"              "Canada/Pacific"                  
## [377] "Canada/Saskatchewan"              "Canada/Yukon"                    
## [379] "CET"                              "Chile/Continental"               
## [381] "Chile/EasterIsland"               "CST6CDT"                         
## [383] "Cuba"                             "EET"                             
## [385] "Egypt"                            "Eire"                            
## [387] "EST"                              "EST5EDT"                         
## [389] "Etc/GMT"                          "Etc/GMT-0"                       
## [391] "Etc/GMT-1"                        "Etc/GMT-10"                      
## [393] "Etc/GMT-11"                       "Etc/GMT-12"                      
## [395] "Etc/GMT-13"                       "Etc/GMT-14"                      
## [397] "Etc/GMT-2"                        "Etc/GMT-3"                       
## [399] "Etc/GMT-4"                        "Etc/GMT-5"                       
## [401] "Etc/GMT-6"                        "Etc/GMT-7"                       
## [403] "Etc/GMT-8"                        "Etc/GMT-9"                       
## [405] "Etc/GMT+0"                        "Etc/GMT+1"                       
## [407] "Etc/GMT+10"                       "Etc/GMT+11"                      
## [409] "Etc/GMT+12"                       "Etc/GMT+2"                       
## [411] "Etc/GMT+3"                        "Etc/GMT+4"                       
## [413] "Etc/GMT+5"                        "Etc/GMT+6"                       
## [415] "Etc/GMT+7"                        "Etc/GMT+8"                       
## [417] "Etc/GMT+9"                        "Etc/GMT0"                        
## [419] "Etc/Greenwich"                    "Etc/UCT"                         
## [421] "Etc/Universal"                    "Etc/UTC"                         
## [423] "Etc/Zulu"                         "Europe/Amsterdam"                
## [425] "Europe/Andorra"                   "Europe/Astrakhan"                
## [427] "Europe/Athens"                    "Europe/Belfast"                  
## [429] "Europe/Belgrade"                  "Europe/Berlin"                   
## [431] "Europe/Bratislava"                "Europe/Brussels"                 
## [433] "Europe/Bucharest"                 "Europe/Budapest"                 
## [435] "Europe/Busingen"                  "Europe/Chisinau"                 
## [437] "Europe/Copenhagen"                "Europe/Dublin"                   
## [439] "Europe/Gibraltar"                 "Europe/Guernsey"                 
## [441] "Europe/Helsinki"                  "Europe/Isle_of_Man"              
## [443] "Europe/Istanbul"                  "Europe/Jersey"                   
## [445] "Europe/Kaliningrad"               "Europe/Kiev"                     
## [447] "Europe/Kirov"                     "Europe/Lisbon"                   
## [449] "Europe/Ljubljana"                 "Europe/London"                   
## [451] "Europe/Luxembourg"                "Europe/Madrid"                   
## [453] "Europe/Malta"                     "Europe/Mariehamn"                
## [455] "Europe/Minsk"                     "Europe/Monaco"                   
## [457] "Europe/Moscow"                    "Europe/Nicosia"                  
## [459] "Europe/Oslo"                      "Europe/Paris"                    
## [461] "Europe/Podgorica"                 "Europe/Prague"                   
## [463] "Europe/Riga"                      "Europe/Rome"                     
## [465] "Europe/Samara"                    "Europe/San_Marino"               
## [467] "Europe/Sarajevo"                  "Europe/Saratov"                  
## [469] "Europe/Simferopol"                "Europe/Skopje"                   
## [471] "Europe/Sofia"                     "Europe/Stockholm"                
## [473] "Europe/Tallinn"                   "Europe/Tirane"                   
## [475] "Europe/Tiraspol"                  "Europe/Ulyanovsk"                
## [477] "Europe/Uzhgorod"                  "Europe/Vaduz"                    
## [479] "Europe/Vatican"                   "Europe/Vienna"                   
## [481] "Europe/Vilnius"                   "Europe/Volgograd"                
## [483] "Europe/Warsaw"                    "Europe/Zagreb"                   
## [485] "Europe/Zaporozhye"                "Europe/Zurich"                   
## [487] "GB"                               "GB-Eire"                         
## [489] "GMT"                              "GMT-0"                           
## [491] "GMT+0"                            "GMT0"                            
## [493] "Greenwich"                        "Hongkong"                        
## [495] "HST"                              "Iceland"                         
## [497] "Indian/Antananarivo"              "Indian/Chagos"                   
## [499] "Indian/Christmas"                 "Indian/Cocos"                    
## [501] "Indian/Comoro"                    "Indian/Kerguelen"                
## [503] "Indian/Mahe"                      "Indian/Maldives"                 
## [505] "Indian/Mauritius"                 "Indian/Mayotte"                  
## [507] "Indian/Reunion"                   "Iran"                            
## [509] "Israel"                           "Jamaica"                         
## [511] "Japan"                            "Kwajalein"                       
## [513] "Libya"                            "MET"                             
## [515] "Mexico/BajaNorte"                 "Mexico/BajaSur"                  
## [517] "Mexico/General"                   "MST"                             
## [519] "MST7MDT"                          "Navajo"                          
## [521] "NZ"                               "NZ-CHAT"                         
## [523] "Pacific/Apia"                     "Pacific/Auckland"                
## [525] "Pacific/Bougainville"             "Pacific/Chatham"                 
## [527] "Pacific/Chuuk"                    "Pacific/Easter"                  
## [529] "Pacific/Efate"                    "Pacific/Enderbury"               
## [531] "Pacific/Fakaofo"                  "Pacific/Fiji"                    
## [533] "Pacific/Funafuti"                 "Pacific/Galapagos"               
## [535] "Pacific/Gambier"                  "Pacific/Guadalcanal"             
## [537] "Pacific/Guam"                     "Pacific/Honolulu"                
## [539] "Pacific/Johnston"                 "Pacific/Kiritimati"              
## [541] "Pacific/Kosrae"                   "Pacific/Kwajalein"               
## [543] "Pacific/Majuro"                   "Pacific/Marquesas"               
## [545] "Pacific/Midway"                   "Pacific/Nauru"                   
## [547] "Pacific/Niue"                     "Pacific/Norfolk"                 
## [549] "Pacific/Noumea"                   "Pacific/Pago_Pago"               
## [551] "Pacific/Palau"                    "Pacific/Pitcairn"                
## [553] "Pacific/Pohnpei"                  "Pacific/Ponape"                  
## [555] "Pacific/Port_Moresby"             "Pacific/Rarotonga"               
## [557] "Pacific/Saipan"                   "Pacific/Samoa"                   
## [559] "Pacific/Tahiti"                   "Pacific/Tarawa"                  
## [561] "Pacific/Tongatapu"                "Pacific/Truk"                    
## [563] "Pacific/Wake"                     "Pacific/Wallis"                  
## [565] "Pacific/Yap"                      "Poland"                          
## [567] "Portugal"                         "PRC"                             
## [569] "PST8PDT"                          "ROC"                             
## [571] "ROK"                              "Singapore"                       
## [573] "Turkey"                           "UCT"                             
## [575] "Universal"                        "US/Alaska"                       
## [577] "US/Aleutian"                      "US/Arizona"                      
## [579] "US/Central"                       "US/East-Indiana"                 
## [581] "US/Eastern"                       "US/Hawaii"                       
## [583] "US/Indiana-Starke"                "US/Michigan"                     
## [585] "US/Mountain"                      "US/Pacific"                      
## [587] "US/Pacific-New"                   "US/Samoa"                        
## [589] "UTC"                              "W-SU"                            
## [591] "WET"                              "Zulu"                            
## attr(,"Version")
## [1] "2018e"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

小結

在這個小節中我們簡介 R 語言的基本資料單位:向量(vector)、如何創建向量以及不同向量類型,包含數值向量(numeric)、整數向量(integer)、文字向量(character)、邏輯值向量(logical)、日期向量(Date)與日期時間向量(POSIXct)。

練習

  • 將您的身高(公分)指派給 my_height;體重(公斤)指派給 my_weight
my_height <- ___
my_weight <- ___
1
2
  • 分別將 my_heightmy_weight 輸出在命令列
___
___
1
2
  • 利用 my_heightmy_weight 計算您的身體質量指數(Body Mass Index,BMI),BMI 計算公式為:

$$BMI = \frac{體重(公斤)}{身高(公尺)^2}$$

bmi <- (___) / (___ / 100)^2
bmi
1
2
  • 將您的姓名指派給 my_name 並且輸出在命令列
my_name <- "___"
my_name
1
2
  • 香港搖滾樂隊 Beyond 於 1983 年成立,我們假設成立日期是 1983-12-31,請將成立日期指派給 beyond_start 並轉換成數值輸出在命令列
beyond_start <- as.Date("___")
as.numeric(___)
1
2
  • 請以系統日期計算今年是 Beyond 成立幾週年紀念?
beyond_start <- as.Date("___")
days_diff <- ___ - ___ # 計算天數差距
years_diff <- ___ / 365 # 除以 365 換算成年
1
2
3

延伸閱讀