Visible to members of this folder
# 在數值上"TRUE"等同於"1";而"FALSE"等同於"0"
> as.numeric(TRUE)
[1] 1
>
> as.numeric(FALSE)
[1] 0
# 簡單的條件判斷
> 1 == 1 # TRUE
[1] TRUE
> 1 < 1 # FALSE
[1] FALSE
> 1 <= 1 # TRUE
> 1 > 1 # FALSE
> 1 >= 1 # TRUE
> 1 != 1 # FALSE
> # 建立一個含有 "1" 的變數
> tocheck <- 1
> # 若 tocheck 等於 "1",顯示 "hello"
> if (tocheck == 1)
+ {
+ print("hello")
+ }
[1] "hello"
> # 現在如果 tocheck 等於 "0",則顯示 "hello"
> if (tocheck == 0)
> # 因為 tocheck 的值為 "1",所以不會顯示任何東西
> # 首先建立函數
> check.bool <- function(x)
+ if (x == 1)
+ # 若輸入值為"1"則顯示"hello"
+ else
+ # 否則顯示"goodbye"
+ print("goodbye")
> # 確認函數能否正確運行
> check.bool(1)
> check.bool(0)
[1] "goodbye"
> check.bool("k")
> check.bool(TRUE) # 因為"TRUE"在數值上等同於"1",所以結果會顯示"hello"
+ # 若輸入值等於"1",顯示"hello"
+ else if (x == 0)
+ # 若輸入值等於"0",顯示"goodbye"
+ # 否則顯示"confused"
+ print("confused")
> check.bool(2)
[1] "confused"
# 使用範例
switch(x, "a" = "first", "b" = "second", "z" = "last", "c" = "third", "other")
# 使用 "switch" 建立函數
> use.switch <- function(x)
+ switch(x, "a" = "first", "b" = "second", "z" = "last", "c" = "third", "other")
> use.switch("a")
[1] "first"
> use.switch("b")
[1] "second"
> use.switch("c")
[1] "third"
> use.switch("d")
[1] "other"
> use.switch("e")
> use.switch("z")
[1] "last"
# 賦予引數數值
> use.switch(1)
> use.switch(2)
> use.switch(3)
> use.switch(4)
> use.switch(5)
> use.switch(6) # 因為"給予的數值"大於"引數的數量",所以不會回傳任何東西
> is.null(use.switch(6)) # 檢測物件是否為"NULL"
ifelse("1 == 1", "Yes", "No")
> # 檢測是否 "1 == 1" ?
> ifelse(1 == 1, "Yes", "No")
[1] "Yes"
> # 檢測是否 "1 == 0" ?
> ifelse(1 == 0, "Yes", "No")
[1] "No"
> toTest <- c(1, 1, 0, 1, 0, 1)
> ifelse(toTest == 1, "Yes", "No")
[1] "Yes" "Yes" "No" "Yes" "No" "Yes"
> # 將檢測的引數用在第二個和第三個引數作為顯示結果
> ifelse(toTest == 1, toTest * 3, toTest)
[1] 3 3 0 3 0 3
> # FALSE 引數中的值會在每次的檢測結果為"FALSE"時顯示
> ifelse(toTest == 1, toTest * 3, "Zero")
[1] "3" "3" "Zero" "3" "Zero" "3"
> # 讓 toTest 包含"NA"元素,其對應的結果為"NA"
> toTest[2] <- NA
[1] "Yes" NA "No" "Yes" "No" "Yes"
[1] 3 NA 0 3 0 3
[1] "3" NA "Zero" "3" "Zero" "3"
> a <- c(1, 1, 0, 1)
> b <- c(2, 1, 0, 1)
> # 使用"單符號"檢測 "a" 和 "b" 的每一個元素
> ifelse(a == 1 & b == 1, "Yes", "No")
[1] "No" "Yes" "No" "Yes"
> # 使用"雙符號"檢測 "a" 和 "b" 的第一個元素,因此只回傳一個結果
> ifelse(a == 1 && b == 1, "Yes", "No")
整理自 R 軟體資料分析基礎與應用 -- Ch09 : 流程控制
(一)if 和 else
# 在數值上"TRUE"等同於"1";而"FALSE"等同於"0"
> as.numeric(TRUE)
[1] 1
>
> as.numeric(FALSE)
[1] 0
>
# 簡單的條件判斷
> 1 == 1 # TRUE
[1] TRUE
>
> 1 < 1 # FALSE
[1] FALSE
>
> 1 <= 1 # TRUE
[1] TRUE
>
> 1 > 1 # FALSE
[1] FALSE
>
> 1 >= 1 # TRUE
[1] TRUE
>
> 1 != 1 # FALSE
[1] FALSE
>
> # 建立一個含有 "1" 的變數
> tocheck <- 1
>
> # 若 tocheck 等於 "1",顯示 "hello"
> if (tocheck == 1)
+ {
+ print("hello")
+ }
[1] "hello"
>
>
> # 現在如果 tocheck 等於 "0",則顯示 "hello"
> if (tocheck == 0)
+ {
+ print("hello")
+ }
> # 因為 tocheck 的值為 "1",所以不會顯示任何東西
>
> # 首先建立函數
> check.bool <- function(x)
+ {
+ if (x == 1)
+ {
+ # 若輸入值為"1"則顯示"hello"
+ print("hello")
+ }
+ else
+ {
+ # 否則顯示"goodbye"
+ print("goodbye")
+ }
+ }
>
>
> # 確認函數能否正確運行
> check.bool(1)
[1] "hello"
>
> check.bool(0)
[1] "goodbye"
>
> check.bool("k")
[1] "goodbye"
>
> check.bool(TRUE) # 因為"TRUE"在數值上等同於"1",所以結果會顯示"hello"
[1] "hello"
>
> check.bool <- function(x)
+ {
+ if (x == 1)
+ {
+ # 若輸入值等於"1",顯示"hello"
+ print("hello")
+ }
+ else if (x == 0)
+ {
+ # 若輸入值等於"0",顯示"goodbye"
+ print("goodbye")
+ }
+ else
+ {
+ # 否則顯示"confused"
+ print("confused")
+ }
+ }
>
> check.bool(1)
[1] "hello"
>
> check.bool(0)
[1] "goodbye"
>
> check.bool(2)
[1] "confused"
>
> check.bool("k")
[1] "confused"
>
(二)switch
# 使用範例
switch(x, "a" = "first", "b" = "second", "z" = "last", "c" = "third", "other")
# 使用 "switch" 建立函數
> use.switch <- function(x)
+ {
+ switch(x, "a" = "first", "b" = "second", "z" = "last", "c" = "third", "other")
+ }
>
> use.switch("a")
[1] "first"
>
> use.switch("b")
[1] "second"
>
> use.switch("c")
[1] "third"
>
> use.switch("d")
[1] "other"
>
> use.switch("e")
[1] "other"
>
> use.switch("z")
[1] "last"
>
# 賦予引數數值
> use.switch <- function(x)
+ {
+ switch(x, "a" = "first", "b" = "second", "z" = "last", "c" = "third", "other")
+ }
>
> use.switch(1)
[1] "first"
>
> use.switch(2)
[1] "second"
>
> use.switch(3)
[1] "last"
>
> use.switch(4)
[1] "third"
>
> use.switch(5)
[1] "other"
>
> use.switch(6) # 因為"給予的數值"大於"引數的數量",所以不會回傳任何東西
> is.null(use.switch(6)) # 檢測物件是否為"NULL"
[1] TRUE
>
(三)ifelse
# 使用範例
ifelse("1 == 1", "Yes", "No")
> # 檢測是否 "1 == 1" ?
> ifelse(1 == 1, "Yes", "No")
[1] "Yes"
>
> # 檢測是否 "1 == 0" ?
> ifelse(1 == 0, "Yes", "No")
[1] "No"
>
> toTest <- c(1, 1, 0, 1, 0, 1)
> ifelse(toTest == 1, "Yes", "No")
[1] "Yes" "Yes" "No" "Yes" "No" "Yes"
>
>
> # 將檢測的引數用在第二個和第三個引數作為顯示結果
> ifelse(toTest == 1, toTest * 3, toTest)
[1] 3 3 0 3 0 3
>
> # FALSE 引數中的值會在每次的檢測結果為"FALSE"時顯示
> ifelse(toTest == 1, toTest * 3, "Zero")
[1] "3" "3" "Zero" "3" "Zero" "3"
>
> # 讓 toTest 包含"NA"元素,其對應的結果為"NA"
> toTest[2] <- NA
> ifelse(toTest == 1, "Yes", "No")
[1] "Yes" NA "No" "Yes" "No" "Yes"
> ifelse(toTest == 1, toTest * 3, toTest)
[1] 3 NA 0 3 0 3
> ifelse(toTest == 1, toTest * 3, "Zero")
[1] "3" NA "Zero" "3" "Zero" "3"
>
(四)複合型條件檢測
> a <- c(1, 1, 0, 1)
> b <- c(2, 1, 0, 1)
>
> # 使用"單符號"檢測 "a" 和 "b" 的每一個元素
> ifelse(a == 1 & b == 1, "Yes", "No")
[1] "No" "Yes" "No" "Yes"
>
> # 使用"雙符號"檢測 "a" 和 "b" 的第一個元素,因此只回傳一個結果
> ifelse(a == 1 && b == 1, "Yes", "No")
[1] "No"
>