Test Dummy
Test Dummy

Reputation: 11

Issue with R Programming. Flipping a weighted coin

Q: Perform 100 repetitions of the experiment of flipping the weighted coin 200 compute the fraction of heads for each experiment, and store the result in a vector y1.

I am using the function replicate but I run into a problem where it will only show me the percent of the 100 repetitions but not each individual flip.

Here is what I have so far.

x= (sample(c(0,1), size=200, prob=c(.10,.90), replace=TRUE))
replicate(x,100) 

Upvotes: 1

Views: 358

Answers (1)

This will do: y1 = rbinom(100,200,0.9)/200

But if you want to use your initial logic:

y1 = apply(replicate(100,sample(0:1, size=200, prob=c(.1,.9), rep=T)),2,sum)/200

Upvotes: 4

Related Questions