To perform a calculation or simulation many times, we can use replicate. But this requires each trial to be independent and identical.

Sometimes, you want expression to depend on iteration. We’ll use for loops in these cases.

Sometimes, we don’t know at outset how many experiments to run. We’ll use while loops in these cases

For Loop

Suppose we want to add the first 100 integers.

current_sum <- 0

for (i in 1:100 ) {
  current_sum <- current_sum + i 
  print( c(i,current_sum) )
}
## [1] 1 1
## [1] 2 3
## [1] 3 6
## [1]  4 10
## [1]  5 15
## [1]  6 21
## [1]  7 28
## [1]  8 36
## [1]  9 45
## [1] 10 55
## [1] 11 66
## [1] 12 78
## [1] 13 91
## [1]  14 105
## [1]  15 120
## [1]  16 136
## [1]  17 153
## [1]  18 171
## [1]  19 190
## [1]  20 210
## [1]  21 231
## [1]  22 253
## [1]  23 276
## [1]  24 300
## [1]  25 325
## [1]  26 351
## [1]  27 378
## [1]  28 406
## [1]  29 435
## [1]  30 465
## [1]  31 496
## [1]  32 528
## [1]  33 561
## [1]  34 595
## [1]  35 630
## [1]  36 666
## [1]  37 703
## [1]  38 741
## [1]  39 780
## [1]  40 820
## [1]  41 861
## [1]  42 903
## [1]  43 946
## [1]  44 990
## [1]   45 1035
## [1]   46 1081
## [1]   47 1128
## [1]   48 1176
## [1]   49 1225
## [1]   50 1275
## [1]   51 1326
## [1]   52 1378
## [1]   53 1431
## [1]   54 1485
## [1]   55 1540
## [1]   56 1596
## [1]   57 1653
## [1]   58 1711
## [1]   59 1770
## [1]   60 1830
## [1]   61 1891
## [1]   62 1953
## [1]   63 2016
## [1]   64 2080
## [1]   65 2145
## [1]   66 2211
## [1]   67 2278
## [1]   68 2346
## [1]   69 2415
## [1]   70 2485
## [1]   71 2556
## [1]   72 2628
## [1]   73 2701
## [1]   74 2775
## [1]   75 2850
## [1]   76 2926
## [1]   77 3003
## [1]   78 3081
## [1]   79 3160
## [1]   80 3240
## [1]   81 3321
## [1]   82 3403
## [1]   83 3486
## [1]   84 3570
## [1]   85 3655
## [1]   86 3741
## [1]   87 3828
## [1]   88 3916
## [1]   89 4005
## [1]   90 4095
## [1]   91 4186
## [1]   92 4278
## [1]   93 4371
## [1]   94 4465
## [1]   95 4560
## [1]   96 4656
## [1]   97 4753
## [1]   98 4851
## [1]   99 4950
## [1]  100 5050

While Loop

How many times do we need to multiply 2 by itself for the product to exceed 1000?

my_product <- 1
steps <- 0

while (my_product <= 1000 ){
  my_product <- my_product*2
  steps <- steps + 1
  print(c(steps, my_product))
}
## [1] 1 2
## [1] 2 4
## [1] 3 8
## [1]  4 16
## [1]  5 32
## [1]  6 64
## [1]   7 128
## [1]   8 256
## [1]   9 512
## [1]   10 1024

Seven Before Nine

Suppose we repeatedly roll a pair of dice. What is the probability of seeing a sum of 7 before a sum of 9?

How do we roll 2 dice and add results?

# One experiment of rolling a pair of dice and summing
sum( sample(1:6, size = 2, replace = T) )
## [1] 6
sum_two <- 0

while (sum_two != 7 & sum_two != 9){
  sum_two <- sum( sample(1:6, size = 2, replace = T) )
}

Use replicate to replicate experiment 10,000

First create function for this experiment.

seven_before_nine <- function(){
  sum_two <- 0

while (sum_two != 7 & sum_two != 9){
  sum_two <- sum( sample(1:6, size = 2, replace = T) )
}
  return(sum_two)
}

Now with replicate

n_times <- 10^4
sum( replicate(n = n_times, seven_before_nine() == 7))/n_times
## [1] 0.602

Is the true probability 60%?