1

I've looked through here to find a solution to my question, but I didn't see one.

In R, I'm trying to simulate Deal or No Deal for a project that gets us familiar with the software. I planned for this to be one big for loop but I can't get my second for loop to work. I broke it apart to make it easier to read (and for me to debug/test).

This loop simulates our original case picked that we keep throughout the game.

    for(i in 1){
    cases <- 1:26
    originalpick <- sample(cases, 1, replace = FALSE) #choose a case
    casesremaining <- (length(cases) - 1) #subtract original case from cases
    }

This loop simulates our first case picked to play.

    for(i in 1){
    casepicked <- replicate(1,sample(cases[-c(originalpick)], 1, replace = FALSE))
   }

My problem is that I am stuck with the possibility that I can choose my original case again. I used sample because I could turn replace off for this very reason, and since that didn't work, I thought that replicate would help. I have seen the update function but I cannot seem to get that to work either.

This is essentially the game, because I have to pick cases until I am left with 2 cases (originally picked case and the one left from cases variable).

I am still fairly new to R, so it could be just my misunderstanding of the functions? Any help would be greatly appreciated!

user210592
  • 13
  • 2
  • Sampling from `cases[-originalpick]`, as you've done, prevents the `originalpick` from being sampled again. Do you mean that in your second loop (which, presumably, will be `for(i in seq_len(casesremaining))`), the `casepicked` is not omitted from subsequent draws? – jbaums Apr 29 '15 at 05:08
  • Maybe you just want `cases <- 1:26; picks <- sample(cases)`, where `picks` is a permutation of `cases` and represents the order in which cases were picked. The first number in `picks` is the case that is kept throughout. Wrap that all in `replicate` if necessary. – jbaums Apr 29 '15 at 05:11
  • @jbaums yes. however, i didn't think of the `for(i in seq_len(casesremaining))` because its going to be inside the other for loop. – user210592 Apr 29 '15 at 05:11
  • @jbaums i tried `casepicked != cases` just to see if it accounted for the first pick and it didn't. Was I wrong in this assumption? – user210592 Apr 29 '15 at 05:13
  • Depends on the context. It would help if you were to edit your question and give a more complete example. `casepicked != cases` will just return a logical vector with 26 elements, where elements are `FALSE` if their value matches `casepicked` and `TRUE` otherwise. Useful functions here might include `%in%` and `setdiff`, but I'm not convinced you need to do it iteratively anyway (see my [previous comment](http://stackoverflow.com/questions/29934741/trying-to-simulate-deal-or-no-deal-in-r#comment47991676_29934741)). – jbaums Apr 29 '15 at 05:15

1 Answers1

2

Assuming you want to simulate the order in which cases are picked, you can do this with a single call to sample (wrapped in replicate if you want to perform multiple simulations).

For example:

set.seed(1)
cases <- 1:26
picks <- sample(cases)

picks
## [1]  7 10 14 21  5 19 23 13 12  2  4  3 25 22 24  6  8  9 16 11 26 17 15  1 18 20

When no additional arguments are provided, sample(x) just permutes the vector x. Above, the case originally picked is case 7, and the remaining elements of picks represent the cases picked subsequently.

To perform multiple simulations:

picks3 <- replicate(3, sample(cases))

##       [,1] [,2] [,3]
##  [1,]    1   12   21
##  [2,]   10    7   25
##  [3,]   21    2   11
##  [4,]    8    3   17
##  [5,]   11   25    9
##  [6,]   13   11    7
##  [7,]   25   14   16
##  [8,]    4    8    4
##  [9,]   15   17   13
## [10,]   12    5    3
## [11,]   24   19   19
## [12,]    2   18   23
## [13,]   22   10   20
## [14,]    6    4    1
## [15,]   20    6    8
## [16,]   23    9   10
## [17,]   14    1   12
## [18,]    5   16   24
## [19,]    9   23   14
## [20,]   16   26   15
## [21,]   26   21    5
## [22,]    3   24   22
## [23,]   17   15   26
## [24,]   19   22    2
## [25,]    7   13   18
## [26,]   18   20    6

Now, each column is an independent simulation of the pick order.


For interest, to simulate one pick at a time with a for loop (e.g. if you want to insert other steps in the process), you could do something like the following (though there are many ways to do it):

picks <- sample(cases, 1) # original pick
for (i in 2:length(cases)) { # subsequent picks
  remaining <- setdiff(cases, picks)
  picks[i] <- remaining[sample(length(remaining), 1)]
}

NB: we use remaining[sample(length(remaining), 1)] instead of just sample(remaining, 1) because at the last iteration, remaining will be a single number (the number of the last remaining case). Passing a single number, e.g. 4, to sample results in sample(1:4, 1) instead of the desired sample(4, 1). See this post for more on that.

Community
  • 1
  • 1
jbaums
  • 27,115
  • 5
  • 79
  • 119