Today’s Goal
- First mission: assemble R code that will simulate rolling a pair of dice.
- Then, we’ll weight the dice a bit in your favor.
Roll a Pair of Dice
## [1] 8
- What is the purpose of the above code?
Roll a Pair of Dice
## [1] 1 2 3 4 5 6
## [1] 6 2
## [1] 8
Roll a Pair of Dice
## [1] 6
## [1] 8
## [1] 6
Roll a Pair of Dice
## [1] 5
## [1] 6
## [1] 8
Writing Your Own Function
- Every function in R has 3 basic parts:
- A name
- A body of code
- A set of arguments
- To create a function in R:
function()
will build a function out of whatever R code you place between the braces.
Writing Your Own Function
## [1] 7
Writing Your Own Function
Writing Your Own Function
- Code that would display a result:
## [1] 1 5
## [1] 3
## [1] 1.414214
Writing Your Own Function
- Code that would NOT display a result:
Function Arguments
## [1] 2
Function Arguments
## [1] 2
## [1] 3
## Error in sample(x = bones, size = 2, replace = TRUE): argument "bones" is missing, with no default
Default Value in Function Arguments
## [1] 8
## [1] 7
Function Arguments

Today’s Goals
- First mission: assemble R code that will simulate rolling a pair of dice.
Next
- We’ll weight the dice a bit in your favor.
- More likely to get #6 than smaller numbers (1, 2, 3, 4, 5).
- How do we do that?
Getting Help in R
Let’s switch over to RStudio!
Sampling with Weights
## [1] 9
## [1] 12
Is It Working?

Is It Working?
