I used sample function in R to generate a sample of 100 fair coin flips, here is the commands i used.
fair.coin = c("heads" = 0.5, "tails" = 0.5)
then,
x <- sample(fair.coin, size = 100, replace = TRUE)
> x
tails tails tails heads tails tails tails heads heads tails heads heads tails tails
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
heads tails heads tails tails tails tails heads heads heads heads tails heads tails
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
tails tails tails heads heads heads heads heads heads heads tails heads tails tails
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
tails heads tails heads heads heads tails tails heads tails tails heads heads heads
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
heads tails tails tails heads heads tails tails tails heads heads heads heads tails
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
tails heads heads heads tails tails tails heads heads heads tails heads tails tails
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
heads tails heads heads tails heads tails tails tails heads heads heads tails heads
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
tails tails
0.5 0.5
That's what I'm getting right now. And I am looking to find the total number of heads that is in this sample, how could I do that? I tried sum(x>0.5)
, x[-tails]
and so on, they are all counting the numbers (since they are fair, all r 0.5), instead of the names of the vector. Is there any functions in R I can use to count the number of heads in this 100 samples?
Thanks in advance!