Watts-College / cpp-527-fall-2021

A course shell for CPP 527 Foundations of Data Science II
https://watts-college.github.io/cpp-527-fall-2021/
2 stars 6 forks source link

step 5# DETERMINE IF CONTENSTANT HAS WON #3

Open kidistbetter105 opened 3 years ago

kidistbetter105 commented 3 years ago

I am getting error at the last two line of code , Am getting this at the bottom of the chunk , Error in sample(x = c(1:3)[-door2], size = 1, replace = F) : object 'door2' not found


determine_winner <- function( final.pick, game )
 {
  WIN <- if( game[ final.pick ] == "car" )
   {  return( "WIN" ) }
  WIN <- if( game[ final.pick ] == "goat" )
   {  return( "LOSE" ) } 
}

this.game <- create_game()
this.game
my.initial.pick <- select_door()
my.initial.pick
opened.goat.door <- open_goat_door( this.game, my.initial.pick )
my.final.pick.stay <- change_door( stay=T, opened.door=opened.goat.door, a.pick=my.initial.pick )
my.final.pick.switch <- change_door( stay=F, opened.door=opened.goat.door, a.pick=my.initial.pick )
lecy commented 3 years ago

The if() statement is a conditional operator, not a regular function. It controls the flow of code (whether certain sections are executed, what happens next, when to break out of a loop, etc), and thus does not have a return value. So your error is trying to assign the outcome of your if() statement to the WIN variable.

#  WIN <- if( game[ final.pick ] == "car" )
if( game[ final.pick ] == "car" )
{  return( "WIN" ) }
lecy commented 3 years ago

It looks like this might be an error from a previous step, though.

Which step does that code come from?

Error in sample(x = c(1:3)[-door2], size = 1, replace = F) : object 'door2' not found
kidistbetter105 commented 3 years ago

Ok it might be.

I got this Error from step 4, and am trying to look back to the previous codes.

Thanks!

kidistbetter105 commented 3 years ago

yes, its step 4 , it keep saying that 'door 2' is not found. what do I have to change to fix the error?

kidistbetter105 commented 3 years ago

our if() now it solved. Thanks Dr. lecy !

lecy commented 3 years ago

yes, its step 4 , it keep saying that 'door 2' is not found. what do I have to change to fix the error?

You would have to explain your Step 4 first :-)

lecy commented 3 years ago

Make sure to paste code and not screen captures otherwise you are asking others to retype your function to test it.

https://stackoverflow.com/help/how-to-ask

kidistbetter105 commented 3 years ago

Make sure to paste code and not screen captures otherwise you are asking others to retype your function to test it.

https://stackoverflow.com/help/how-to-ask

OK! Sorry about that !

lecy commented 3 years ago

Missing value where TRUE/FALSE needed means this statement is not functioning properly:

game[ final.pick ] == "car"

I suspect it is a problem with Step 4 but I would need more to help.

For a reproducible example you would need to provide the values of the objects used as inputs for determine_winner():

# what are the values of these objects? 

this.game
my.initial.pick
my.final.pick

determine_winner( final.pick=my.final.pick, 
                  game=this.game )
lecy commented 3 years ago

What are these values, though?

this.game
my.initial.pick
my.final.pick 
opened.door 
lecy commented 3 years ago

Object values would look something like this:

> this.game
[1] "goat" "car"  "goat"
> my.initial.pick
[1] 2
lecy commented 3 years ago

Ok, I think I see what's happening. Doors is numeric and your pick is a number so you should not be recasting them here.

doors != as.character( a.pick )

Once you do that you are triggering an implicit cast for the comparison (you can't determine if numbers are equal to letters without recasting the numbers as letters). So then doors becomes a character vector. When you recast as numeric later you might be creating the NAs.

doors <- doors[ doors!= as.character(a.pick) & doors !="car" ]

But more importantly, this comparison will return FALSE FALSE FALSE because doors is [ 1 2 3 ] so no element equals "car".

doors != "car"    # doors[ doors!= as.character(a.pick) & doors !="car" ]

You need to use the game vector for that comparison.

game != "car"   
lecy commented 3 years ago

One more - doors are door numbers used for comparisons with contestant selections (they specify door numbers, not goats or cars, in their selection).

You need to create the doors vector in the function.

# try this
doors <- c(1,2,3)

# not this 
# doors <- game
lecy commented 3 years ago

Practice "breaking open" your functions to test them.

Assign reasonable values to the arguments then run the code inside the function as a regular script.

Check the output at each step. It's the only way you will figure out where things are going wrong.

game <- c("goat","goat","car")
a.pick <- 2

# open_goat_door <- function( game, a.pick )
# {

   doors <- c(1,2,3)
   doors <- doors[ doors != a.pick & game != "car" ]
   doors
   opened.door <- sample( doors, size=1, replace=F )
   opened.door 

#   return( opened.door ) # number between 1 and 3  
# }
lecy commented 3 years ago

Excellent.

One quick note on code formatting - for a chunk you need three ticks, not one.

doors <- c(1,2,3) doors <- doors[ doors != a.pick & game != "car" ]

If you add the language to the ticks it will use syntax highlighting specific to that language.

```r
doors <- c(1,2,3)
doors <- doors[ doors != a.pick & game != "car" ]

With "r":

```r
doors <- c(1,2,3)
doors <- doors[ doors != a.pick & game != "car" ]

Versus without the "r":

doors <- c(1,2,3)
doors <- doors[ doors != a.pick & game != "car" ]
lecy commented 3 years ago

You are very welcome!