December 5, 2016

大綱

  • 甚麼是迴圈
  • 迴圈類型
    • For
    • While
    • Repeat
  • 輔助function
    • break
    • next
  • 檢查效能
    • system.time

What Are Loops?

直接用例子說明–Fibonacci series

1, 1, 2, 3, 5, 8, 13, 21, 34

(a = c(1,1))
## [1] 1 1
(a = c(a,2))
## [1] 1 1 2
(a = c(a,a[2]+a[3]))
## [1] 1 1 2 3

(a = c(a,a[3]+a[4]))
## [1] 1 1 2 3 5
(a = c(a,a[4]+a[5]))
## [1] 1 1 2 3 5 8
(a = c(a,a[5]+a[6]))
## [1]  1  1  2  3  5  8 13

如果數列長度為10000?

遇到這種有規律、重複、循環動作的事情 –> Loop

剛才的Fibonacci series

a = c(1,1)
for (i in 1:50){
        a = c(a, a[i]+a[i+1])
}
print(a)
##  [1]           1           1           2           3           5
##  [6]           8          13          21          34          55
## [11]          89         144         233         377         610
## [16]         987        1597        2584        4181        6765
## [21]       10946       17711       28657       46368       75025
## [26]      121393      196418      317811      514229      832040
## [31]     1346269     2178309     3524578     5702887     9227465
## [36]    14930352    24157817    39088169    63245986   102334155
## [41]   165580141   267914296   433494437   701408733  1134903170
## [46]  1836311903  2971215073  4807526976  7778742049 12586269025
## [51] 20365011074 32951280099

R迴圈類型

For Loop

y = 0
for (x in 1:10){ #重覆動作多少次
        y = y + x
        print(y)
}
## [1] 1
## [1] 3
## [1] 6
## [1] 10
## [1] 15
## [1] 21
## [1] 28
## [1] 36
## [1] 45
## [1] 55

While Loop

y = 0
x = 1
while(x <= 10){ #條件符合就繼續執行
     y = y + x
     x = x + 1
     print(y)
}
## [1] 1
## [1] 3
## [1] 6
## [1] 10
## [1] 15
## [1] 21
## [1] 28
## [1] 36
## [1] 45
## [1] 55

Repeat Loop

y = 0
x = 1
repeat{ #不斷重覆,直到永遠
        y = y + x
        x = x + 1
        if (x > 10){
                break #停止迴圈
        }
        print(y)
}
## [1] 1
## [1] 3
## [1] 6
## [1] 10
## [1] 15
## [1] 21
## [1] 28
## [1] 36
## [1] 45

Repeat Loop

如果repeat{}沒有break,就像脫韁之馬

y = 0
x = 1
repeat{ #不斷重覆,直到你手動暫停或電腦關機
        y = y + 1
        x = x + 1
        print(y)
}

自己試試看

next

y = 0
for (x in 1:10){ #重覆動作多少次
        if (x == 3) next #跳至下一個迴圈
        y = y + x
        print(y)
}
## [1] 1
## [1] 3
## [1] 7
## [1] 12
## [1] 18
## [1] 25
## [1] 33
## [1] 42
## [1] 52

system.time

y = 0
system.time( #測量時間(秒)
        for (x in 1:10000){
        y = y + x
        }
)
##    user  system elapsed 
##   0.004   0.000   0.002

  • for
  • while
  • repeat

  • next
  • break

  • system.time

避免迴圈

t1 = NULL
system.time(
        for (i in 1:10000){
                t1 = c(t1,i)
        }
)
##    user  system elapsed 
##   0.124   0.000   0.121
system.time(1:10000)
##    user  system elapsed 
##       0       0       0

避免迴圈

t1 = numeric(10000)
system.time(
        for (i in 1:10000){
                t1[i] = i
        }
)
##    user  system elapsed 
##   0.008   0.000   0.010
system.time(1:10000)
##    user  system elapsed 
##       0       0       0

總結

  • Try & Error

Homework

  1. 寫一個function,找出[a,b]之間所有質數

  2. 找套件自學,寒假回來分享
    • 可以多個套件
    • 介紹至少5-20分鐘

寒假回來第一次Meeting交

References

Advanced

Advanced Topics

  • Recursive

  • How to make loops run faster?

  • Profiling of R's execution.

  • If error, then next().

References