Trying to calculate a probability (REPREX)

# Assign a variable 'n' as the number of remaining games.
n <- 6

# Assign a variable 'l' to a list of possible game outcomes, where 0 indicates a loss and 1 indicates a win for the Cavs. 
l <- list(0:1) 

# Create a data frame named 'possibilities' that contains all possible outcomes for the remaining games.
possibilities <- data.frame(expand.grid(rep(l, n)))   

# Create a vector named 'results' that indicates whether each row in the data frame 'possibilities' contains enough wins for the Cavs to win the series.
results <- rowSums(possibilities[which(1 >= 4)])       

# Calculate the proportion of 'results' in which the Cavs win the series. Print the outcome to the console.
#> [1] 0

What I am trying to do -->> Two teams, say the Cavs and the Warriors, are playing a seven game championship series. The first to win four games wins the series. The teams are equally good, so they each have a 50-50 chance of winning each game.

My desired output -->> If the Cavs lose the first game, what is the probability that they win the series?

I feel pretty confident with the first three lines and the last line of meaningful code. My definition of the "results" variable is where I am not sure. I am trying to check for the number of rows in the "possibilties" data frame where the number of "1s" for wins is equal to or greater than 4 (four). any ideas ? thanks

This is what you want.

results <- rowSums(possibilities) >= 4

Note that the function rowSum will just sum the values in each of the rows in your 'possibilities' vector. What you are trying to do is check which of those rows have a sum higher than 4. That is why the logical >= is outside the function.


got it. thank you. the cavs cant beat the warriors. lol


If your question has been answered, can you mark your question as solved

It will help other knows the status of the question right from the list of question or by searching for related topic.

Thank you.

1 Like