Hello,
Try the following.
fun <- function(n = 100){
x <- sample(c("H","T"),3*n,replace=TRUE)
dim(x) <- c(3,n)
num_heads <- apply(x,2,function(x) sum(x=="H"))
table(num_heads)/n
}
Runs <- 1e1
t(replicate(Runs, fun()))
Hope this helps,
Rui Barradas
Em 09-10-2012 06:04, David Ar
On Tue, Oct 9, 2012 at 6:04 AM, David Arnold wrote:
> All,
>
> I've devised a way to toss a coin three times and record the number of heads,
> then use the table command to summarize the number of time 0, 1, 2, or 3
> heads were obtained.
>
> n <- 100
> x <- sample(c("H","T"),3*n,replace=TRUE)
>
All,
I've devised a way to toss a coin three times and record the number of heads,
then use the table command to summarize the number of time 0, 1, 2, or 3 heads
were obtained.
n <- 100
x <- sample(c("H","T"),3*n,replace=TRUE)
dim(x) <- c(3,n)
num_heads <- apply(x,2,function(x) sum(x=="H"))
tab
3 matches
Mail list logo