In this R Tutorial, we will complete Powerball data analysis based on the odds of winning the Powerball. Playing the Powerball is only a mere $2 a ticket, which is half the cost for a cup of coffee. Now if that $2 ticket hit the Jackpot then the reward would be most benefiting.

### So what are the odds of winning?

- Grand Prize -> Match 5 white balls and the Powerball -> 1 in 292,201,338.00
- $1,000,000 -> Match 5 white balls -> 1 in 11,688,053.52
- $50,000 -> Match 4 white balls and the Powerball -> 1 in 913,129.18
- $100 -> Match 4 white balls -> 1 in 36,525.17
- $100 -> Match 3 white balls and the Powerball -> 1 in 14,494.11
- $7 -> Match 3 white balls -> 1 in 579.76
- $7 -> Match 2 white balls and the Powerball -> 1 in 701.33
- $4 -> Match 1 white ball and the Powerball -> 1 in 91.98
- $4 -> Match the Powerball -> 1 in 38.32

So the odds of winning the Grand Prize are **1:292,201,338** but it would cost double to buy all variations of the tickets since each ticket is $2.

## What is the Powerball?

The Powerball is made of 69 white balls and only 5 will be drawn. In addition, there are 26 red balls (Powerball) which will total 6 balls that need to match the drawing in order to win the Grand Prize. The Powerball drawings are every Wednesday and Saturday night at **10:59 p.m. EST (Eastern Standard Time AMERICAS).** we draw five white balls out of a drum with 69 balls and one red ball out of a drum with 26 red balls.

## Creating Powerball Match and Prize Variables

Below are the variables that are created from top prize to no prize and the corresponding output for each.

### Match 5 white balls and the Powerball

1 |
> white_5_red_1 <- c(5,1) |

### One Winner for the total Grand Prize

1 |
> grand_prize <- "You won the Grand Prize of $900,000,000" |

### Match 5 white balls and 0 Powerball

1 |
> white_5_red_0 <- c(5,0) |

### Second Powerball prize of $1,000,000

1 |
> second_prize <- "You won the Grand Prize of $1,000,000" |

### Match 4 white balls and the Powerball

1 |
> white_4_red_1 <- c(4,1) |

### Third Powerball prize of $50,000

1 |
> third_prize <- "You won the Grand Prize of $50,000" |

### Match 4 white balls and 0 Powerball

1 |
> white_4_red_0 <- c(4,0) |

### Fourth Powerball prize of $100

1 |
> fourth_prize <- "You won the Grand Prize of $100" |

### Match 3 white balls and the Powerball

1 |
> white_3_red_1 <- c(3,1) |

### Fifth Powerball prize of $100

1 |
> fifth_prize <- "You won the Grand Prize of $100" |

### Match 3 white balls and 0 Powerball

1 |
> white_3_red_0 <- c(3,0) |

### Sixth Powerball prize of $100

1 |
> sixth_prize <- "You won the Grand Prize of $7" |

### Match 2 white balls and the Powerball

1 |
> white_2_red_0 <- c(2,0) |

### Seventh Powerball prize of $7

1 |
> seventh_prize <- "You won the Grand Prize of $17" |

### Match 1 white ball and the Powerball

1 |
> white_1_red_1 <- c(1,1) |

### Eighth Powerball prize of $4

1 |
> eighth_prize <- "You won the Grand Prize of $4" |

### Match 0 white balls and the Powerball

1 |
> white_0_red_1 <- c(0,1) |

### Ninth Powerball prize of $4

1 |
> ninth_prize <- "You won the Grand Prize of $4" |

### Match 0 white balls and the Powerball

1 |
> white_0_red_0 <- c(0,0) |

### No Powerball prize

1 |
> no_prize <- "No luck this time, try again" |

## R else if() statements

In order to create a function statement to verify which prize a person may win, a functional **else if()** statement will work. The function works because it will allow several conditions to be checked. We will be able to use the above variables and creation a function that will print which prize you won or didn’t win.

1 2 3 4 5 6 7 8 9 10 11 |
> powerball_numbers <- function(x) {if(identical(x,white_5_red_1)){grand_prize} else if(identical(x,white_5_red_0)){second_prize} else if(identical(x,white_4_red_1)){third_prize} else if(identical(x,white_4_red_0)){fourth_prize} else if(identical(x,white_3_red_1)){fifth_prize} else if(identical(x,white_3_red_0)){sixth_prize} else if(identical(x,white_2_red_0)){seventh_prize} else if(identical(x,white_1_red_1)){eighth_prize} else if(identical(x,white_0_red_1)){ninth_prize} else if(identical(x,white_0_red_0)){no_prize} } |

Now with the above function, we can manually input the white ball and red ball variable to match the number of white balls and the Powerball.

1 2 |
> powerball_numbers(c(5,1)) [1] "You won the Grand Prize of $900,000,000" |

Let’s try two more but pick 0-5 for the white balls and either 0 or 1 for the Powerball.

1 2 |
> powerball_numbers(c(3,0)) [1] "You won the Grand Prize of $7" |

1 2 |
> powerball_numbers(c(0,0)) [1] "No luck this time, try again" |

## Calculate the Powerball Odds

If we’re looking to win the Powerball second prize, we would need to match the 5 white numbers that will be drawn. There are exactly 5 * 4 * 3 * 2 * 1 different orders that are known as 5 factorial.

### Mathematical choose() Function

The **choose()** function returns binomial coefficients and the logarithms of their absolute values. The **choose()** function will be used because it is defined for all real numbers **n** and integer **k** **(choose(n, k))**.

While utilizing this function, we can start to build variables based on the Grand Prize and the count of white balls and red balls (Powerball).

#### So how we do we calculate?

Since the order would be 5 * 4 * 3 * 2 * 1 with a total of 69 white balls then the probability of matching all of the main numbers in Powerball looks like

1 2 |
> choose(69,5)*26 [1] 292201338 |

This shows us that the odds to match all 5 white balls plus the Power ball will be 1 in 292,201,338.