0

I am new to R and I have a small issue. I have the following code for my slot machine in R (from the "hands-on programming with R" from Garrett Grolemund.

When I use the play() function for my slot machine that you can see below, I get a single number that tells me how much I have won. When I use the following code to replicate the outcome:

slot.mach <- replicate(1000,play())

R prints all the 1000 examples in my console. What I don't understand is that when I use the same replicate function on my pair of dice that also have one number as an outcome, it does not print all the rolls, but instead just adds them as a vector to my object and I can use qplot right away (that's what I want with my slot machine too).

I obviously don't want all of them printed as it takes a lot of time & space and is unnecessary for me to see all the outcomes.

My guess is that it might have something to do with the class and type of the object, see below:

[1] "integer"
> typeof(slot.mach)
[1] "double"
> class(slot.mach)
[1] "numeric"
> typeof(roll.dice) 
[1] "integer"
> class(roll.dice)
[1] "integer"

I don't know whether it's important for you to help me, but here is the whole code for my slot machine:

score <- function(symbols) {

#identify case of symbols frequency

 same <- length(unique(symbols)) == 1
 bars <- symbols %in% c("B","BB","BBB")

#get prize

 if (same) {
 payouts <- c("DD" = 100,"7" = 80, "BBB" = 40, "BB" = 25, "B" = 10, "C" = 10, 
 "0" = 0)
 prize <- unname(payouts[symbols[1]])
 } else if (all(bars)){
 prize <- 5
 } else {
  cherries <- sum(symbols == "C")
    prize <- c(0,2,5)[cherries+1]
  } 

#adjust for diamonds

 diamonds <- sum(symbols == "DD")
 prize <- prize*(2^diamonds)


}

Then I can play the function:

play <- function() {

 get_symbols <- function() {
 wheel <- c("DD","7","BBB","BB","B","C","0")
 sample(wheel,size=3,replace=TRUE,
       prob=c(0.03,0.03,0.06,0.1,0.25,0.01,0.52))

symbols <- get_symbols()
print(score(symbols))
}

Lastly, I get a print outcome, so every "play()" gives me a number. Usually it's zero, sometimes it's a small amount (you can see the probabilities above).

here's my dice function

roll.dice <- function(){x <- 1:6
y <- c(1/8,1/8,1/8,1/8,1/8,3/8)
dice<-sample(x,size=2,replace=TRUE,prob=y)
sum(dice)}
roll.dice <- replicate(10000,roll.dice())
JachymDvorak
  • 129
  • 8
  • Your `play` function is incomplete here. You're either missing a closing brace for `get_symbols`, or for `play`. – De Novo Mar 23 '18 at 23:07
  • Oh and one more thing... Welcome to stack overflow, and good work writing your first question correctly! Yes, it IS important to include the definition of the user defined functions being called when you generate the error. Upvoting! – De Novo Mar 23 '18 at 23:14

2 Answers2

1

replicate is printing because you are replicateing a function that has a call to print

Change the print call to a return, and you'll get an array of scores in the object you're assigning to the return value of replicate instead.

Adding in a presumed missing closing brace around the definition of get_symbols, you get:

play <- function() {

  get_symbols <- function() {
   wheel <- c("DD","7","BBB","BB","B","C","0")
   sample(wheel,size=3,replace=TRUE,
       prob=c(0.03,0.03,0.06,0.1,0.25,0.01,0.52))
  }
symbols <- get_symbols()
score(symbols)
}

Run that with your other functions as written, and when you call: roll.dice <- replicate(10000,roll.dice()), you'll get an integer vector of length 10000, containing the outcomes of 10000 roles of dice, with nothing printed to the console.

De Novo
  • 7,120
  • 1
  • 23
  • 39
0

Like @DanHall said, you do not want to print your results. I have modified your functions to what you are looking for. Just a quick note, functions are really useful because we only need to define them once, and we can call them as much as we want in a much more visually appealing way. Your get_symbols function is being defined every time you run play, and that is unnecessary. You want your code to look like,

score <- function(symbols) {

  #identify case of symbols frequency

  same <- length(unique(symbols)) == 1
  bars <- symbols %in% c("B","BB","BBB")

  #get prize

  if (same) {
    payouts <- c("DD" = 100,"7" = 80, "BBB" = 40, "BB" = 25, "B" = 10, "C" = 10, 
                 "0" = 0)
    prize <- unname(payouts[symbols[1]])
  } else if (all(bars)){
    prize <- 5
  } else {
    cherries <- sum(symbols == "C")
    prize <- c(0,2,5)[cherries+1]
  } 

  #adjust for diamonds

  diamonds <- sum(symbols == "DD")
  prize <- prize*(2^diamonds)

  prize
}
get_symbols <- function() {
  wheel <- c("DD","7","BBB","BB","B","C","0")
  sample(wheel,size=3,replace=TRUE,
         prob=c(0.03,0.03,0.06,0.1,0.25,0.01,0.52))
}
play <- function() {
    symbols <- get_symbols()
    score(symbols)
}
smanski
  • 541
  • 2
  • 7