[R] runs of heads when flipping a coin

jim holtman jholtman at gmail.com
Thu Oct 9 19:26:57 CEST 2008


You can use 'sample' and 'rle':

> x <- sample(c("H","T"), 500, replace=TRUE, prob=c(.95, .05))
> as.data.frame(unclass(rle(x)))
   lengths values
1       12      H
2        1      T
3       10      H
4        1      T
5        5      H
6        2      T
7       26      H
8        1      T
9       17      H
10       1      T
11      54      H
12       1      T
13      23      H
14       1      T
15       7      H
16       1      T
17       2      H


On Thu, Oct 9, 2008 at 1:16 PM, Harvey <modelers at gmail.com> wrote:
> Can someone recommend a method to answer the following type of question:
>
> Suppose I have a coin with a probability hhh of coming up heads (and 1-hhh
> of coming up tails)
> I plan on flipping the coin nnn times (for example, nnn = 500)
> What is the expected probability or frequency of a run of rrr heads* during
> the nnn=500 coin flips?
> Moreover, I would probably (excuse the pun) want the answer for a range of
> rrr values, for example rrr = 0:50
>
> Of course I am more interested in an analytical solution than a monte carlo
> simulation solution.
>
> Thanks in advance,
>
> Harvey
>
> * OR "a run of rrr heads or more ..."
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list