R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#creating list of roulette rounds
roulette_results1 <- sample(1:38,size=5000,replace=TRUE)

#just for fun I'll use the actual red numbers in roulette, treating 37 and 38 and 0 and 00 respectively.
red_wins <- c(1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36)

#making it into a dataframe
roulette_df1 <- data.frame(roulette_results1)
names(roulette_df1)[1] <- "roulette_results"

#setting the condition: +1 if I won, -1 if I lost.
roulette_df1$net_dollars <- ifelse(roulette_df1$roulette_results %in% red_wins, 
                                       1, -1)

#creating a cumulative sum, AKA my running winnings tally.
roulette_df1$cumsum_dollars <- cumsum(roulette_df1$net_dollars)

roulette_df1 %>%
  head(10)
##    roulette_results net_dollars cumsum_dollars
## 1                14           1              1
## 2                13          -1              0
## 3                29          -1             -1
## 4                33          -1             -2
## 5                35          -1             -3
## 6                31          -1             -4
## 7                12           1             -3
## 8                33          -1             -4
## 9                10          -1             -5
## 10               30           1             -4
ggplot(roulette_df1, aes(x = seq_along(cumsum_dollars), y = cumsum_dollars)) + 
  geom_line() + 
  xlab('Roulette Tries') + 
  ylab('Net $') + 
  ggtitle('My Net "Winnings" in Roulette (Betting on Red)')

#The number the roullette results must match in order for me to win
#In my index, I'm treating 37 as '0' and 38 as '00'
my_lucky_number <- 17

#creating list of roulette rounds
roulette_results2 <- sample(1:38,size=5000,replace=TRUE)

#making it into a dataframe
roulette_df2 <- data.frame(roulette_results2)
names(roulette_df2)[1] <- "roulette_results"

#setting the condition: +35 if I won, -1 if I lost.
roulette_df2$net_dollars <- ifelse(roulette_df2$roulette_results == my_lucky_number, 
                                       35, -1)

#creating a cumulative sum, AKA my running winnings tally.
roulette_df2$cumsum_dollars <- cumsum(roulette_df2$net_dollars)

roulette_df2 %>%
  head(10)
##    roulette_results net_dollars cumsum_dollars
## 1                 5          -1             -1
## 2                23          -1             -2
## 3                16          -1             -3
## 4                34          -1             -4
## 5                10          -1             -5
## 6                17          35             30
## 7                20          -1             29
## 8                 5          -1             28
## 9                 1          -1             27
## 10               24          -1             26
ggplot(roulette_df2, aes(x = seq_along(cumsum_dollars), y = cumsum_dollars)) + 
  geom_line() + 
  xlab('Roulette Tries') + 
  ylab('Net $') + 
  ggtitle('My Net "Winnings" in Roulette (Betting on 17)')