Complete sections 1 (quick refresher), 2 (when and how you should write a function) and 3 (functional programming) of the “Writing Functions in R” course at DataCamp. If you’ve already done this in the past, then your account at DataCamp should reflect this. If for some reason it doesn’t, then you can show me that you’ve done it.
Exercise 5.7.1. Note that you are asked here to write a function and not simply an expression.
Exercise 5.7.2
Exercise 5.7.3. You can omit part (d) but you should read the rationale.
Simulate one round of the game described in the problem. This shouldn’t be a function.
Now a function:
To be clear, this function will provide an estimate of the probability of winning (getting at least one six in n rolls) by simulating the game N times and calculating the proportion of wins. The output should be a vector containing the estimate, the exact probability and the difference between them.
Exercise 5.7.5
What will be the output of the following code? Try to answer before you run the code.
rabbit <- function(n) { if (n == 1 || n == 2) { 1 } else { rabbit(n - 2) + rabbit(n - 1) } } rabbit(10)
Exercise 5.7.7