學習R之流程控制和迴圈

2021-10-22 05:38:08 字數 4005 閱讀 6046

在執行**時,並不僅僅是逐行執行,而需要更好地控制執行流程,這就涉及到流程控制語句。

1.1 if 和 else

最簡單的流程控制語句是 if 語句,if 接受乙個邏輯值,該值為 ture 時才會執行下一條語句。

當然,大部分時候都不會直接傳入 true 或 false 值,而是傳遞乙個變數或表示式,滿足 if 語句的條件才會繼續執行。

# 這條語句會執行if(

true

)>

> it was true!

# 這條語句不會執行if(

false

)# x > 2 時執行 if 語句

x <-3if

(x >2)

>

> y =

6, z =

18

與 if 對應的是 else 語句,如果 if 的條件值為 false,則會執行 else 之後的**:

x <- runif(1)

if(x >

0.5)

else

>

> x =

0.432472578017041 less then 0.5

可以反覆使用 if 和 else 來定義多個條件:

r <- runif(2)

x < r[1]

/ r[2]

>

>[1

]true

if(is.nan(x)

)else

if(is.infinite(x)

)else

if(x >0)

else

if(x <0)

else

>

> x is positive

1.2 向量化的 if

標準的 if 語句需要乙個邏輯值作為引數,如果給它傳遞乙個長度超過 1 的邏輯向量,r 會警告你已給出多個選項,僅第乙個將被使用。

這時可以使用 ifelse 函式,它有三個引數:第乙個是邏輯條件向量,第二個引數值在第乙個向量為 true 時被返回,第三個引數值在第乙個向量為 false 時被返回。

# 不要這麼使用 if

if(c(

true

,false))

>

> warning in

if(c(

true

,false))

))>

>[1

]3# 沒有匹配值時,返回沒有命名的值 4

(greek <- switch(

"delta"

, alpha =1,

beta = sqrt(4)

, gamma =,4

))>

>[1

]4

r 中有三種迴圈:repeat、while 和 for,雖然向量化意味著並不像其他語言一樣大量需要它們,但在需要重複執行**時,迴圈還是很有用的。

2.1 repeat 迴圈

repeat 迴圈就是反覆地執行**,直到告訴它停為止,通常使用 break 語句跳出整個迴圈,使用 next 跳出當前迴圈。

# 隨機抽到字母 c 則退出整個迴圈

i =0

repeat

else

}>

> the letter is a

>

> the letter is b

>

> the letter is c

message(i)

>

>

2# 隨機抽到字母 b 則跳出當前迴圈 j 不累加,抽到 c 則退出整個迴圈

j <-

0repeat

else

if(letter ==

"c")

else

}>

> the letter is a

>

> the letter is a

>

> the letter is c

message(j)

>

>

2

2.2 while 迴圈

while 迴圈就像是延遲了的 repeat 迴圈,先進行檢查,若滿足條件則執行**,否則不執行**。

letter <- sample(c(

"a",

"b",

"c",

"d"),1

)message(

"the letter is "

, letter)

>

> the letter is d

# 隨機抽到的字母不是 c 則執行 while 迴圈

while

(letter !=

"c")

>

> the letter is a

>

> the letter is b

>

> the letter is a

>

> the letter is a

>

> the letter is a

>

> the letter is a

>

> the letter is d

>

> the letter is b

>

> the letter is d

>

> the letter is a

>

> the letter is c

2.3 for 迴圈

for 迴圈適用於已知**所需執行的迴圈次數的情形,它將接受乙個迭代器變數和乙個向量引數,在每個迴圈中,迭代器變數會從向量中取得乙個值。

r 的 for 迴圈非常靈活,輸入並不限於整數或數字,還可以傳入字元向量、邏輯向量或列表:

# 整數

for(i in1:

5)>

> i =

1>

> j =

1>

> i =

2>

> j =

4>

> i =

3>

> j =

9>

> i =

4>

> j =

16>

> i =

5>

> j =

25# 字元向量

for(month in month.name)

>

> the month of january

>

> the month of february

>

> the month of march

>

> the month of april

>

> the month of may

>

> the month of june

>

> the month of july

>

> the month of august

>

> the month of september

>

> the month of october

>

> the month of november

>

> the month of december

# 邏輯向量

for(yn in c(

true

,false,na

))>

> this statement is true

>

> this statement is false

>

> this statement is na

需要注意的是,r 的 for 迴圈幾乎總是比其對應的向量化執行得要慢,而且往往是一到兩個數量級的差別,所以盡可能地使用向量化而不是迴圈。

Scala之流程控制語句for迴圈

for迴圈本質是一種遍歷,遍歷集合中的每個元素,當遍歷完最後乙個元素的時候,自動終止.val a abc 字串 遍歷容器.變數c是常量,不能修改 for c a 結果 a bc字元相加就變成int型別了 val a abc 字串 遍歷容器.變數c是常量,不能修改 for c a 97 9899三種寫...

MySQL學習之流程控制

流程控制 case when then else end case test when val1 then result else default end 如果test和valn相等,則返回resultn,否則返回default if isnull t,f 如果test是真,返回t 否則返回f 例如...

JS學習之流程控制

doctype html utf 8 流程控制 分支 title 的語法結構 if 條件表示式 if 3 5 3.案例 進入網咖 彈出乙個輸入框。要求使用者輸入年齡,如果年齡大於等於18歲,允許進入網咖 var age prompt 請輸入您的年齡 if age 18 else 案例 判斷閏年 接受...