This is my first post here and I couldn't find the answer I was looking for. I'm currently taking edX course on Probability in Data Science, but I got stuck on section 1. The task asks you to simulate a series of 6 games with random, independent outcomes of either a loss (0) or win(1), and then use the sum function to determine whether a simulated series contained at least 4 wins. Here's what I did:
l <- list(0:1)
n <- 6
games <- expand.grid(rep(l, n))
games <- paste (games$Var1, games$Var2, games$Var3, games$Var4, games$Var5, games$Var6)
sample (game, 1, replace = TRUE)
but I can't seem to use the sum function to sum the result of '''sample''' and check if there's a series of at least 4 games. I've been trying to use
sum(sample (game, 1, replace = TRUE))
but can't seem to get anywhere with it.
Any light would be greatly appreciated! Thanks a lot!
This is what one simulated series look like
sample(c(0, 1), 6, replace = TRUE)
To count number of wins (i.e 1) you could use sum
like
sum(sample(c(0, 1), 6, replace = TRUE)) >= 4
Now you could generate such series n
times with replicate
.
n <- 1000
replicate(n, sum(sample(c(0, 1), 6, replace = TRUE)) >= 4)
If you have to use games
to calculate you can use rowSums
to count number of 1's
sum(rowSums(games) >= 4)
#[1] 22