Reading Questions Answer Checker
To check your answers put them in the appropriate box and click the 'Check' button. Every checker box can do arithmetic and calculate standard functions (see calculator help). If you give decimal answers, give them to at least 3 decimal places.
As you work you should have pencil and paper handy for calculations and thinking!
Note: some questions ask for a formula. For the checker we ask you to plug a value into the formula. For your pset you still need to give the whole formula.
Calculator
=
Problem 1.
Using what you've learned about R, simulate rolling one die 100000 times and compute the average of the 100000 rolls.
(You should use the sample() and mean() functions.)
Problem 2.
In a sequence of numbers a run is a subsequence of the same number. For example
(1,1,1,2,3,3,3,1,1)
has a run of 1's of length 3, a run of 2's of length 1, a run of 3's of length 3 and a run of 1's of length 2.
The R function rle() helps compute the run lengths in a sequence. Try running the following code
> x = c(1,1,1,2,3,3,3,1,1)
> y = rle(x)
> y
You should see the following
Run Length Encoding
lengths: int [1:4] 3 1 3 2
values : num [1:4] 1 2 3 1
The lengths vector shows the runs in x
and the values vector shows the number used for each run. To pick out just the lengths vector you use the command: y$lengths
You should see: [1] 3 1 3 2
Run the code
set.seed(10)
y = rbinom(20,1,.5)
y
is a vector of 0's and 1's of length 20. Use rle(y)
to find the length of the longest run in y.