Watts-College / cpp-527-spr-2022

https://watts-college.github.io/cpp-527-spr-2022/
0 stars 1 forks source link

Lab 01 - Step 5 #9

Open ellihammons21 opened 2 years ago

ellihammons21 commented 2 years ago

Hi everyone!

I'm working through Lab 01, and everything seems to work fine until Step 5. This section of the code is taken directly from the instructions, and I'm fairly certain that I've set up the win/lose function correctly, so I'm not sure what's going wrong.


determine_winner <- function( my.final.pick, game )
{

   if( game[ my.final.pick ] == "car"  )
   {
      return( "WIN" )
   }
   if( game[ my.final.pick ] == "goat" )
   {
      return( "LOSE" )
   }

}

Then when I test this function, as shown below, I get the following error code:

the condition has length > 1 and only the first element will be usedError in determine_winner(final.pick = my.final.pick, game = this.game) : unused argument (final.pick = my.final.pick)

Specifically, it is throwing the error on this line of code: determine_winner( final.pick=my.final.pick, game=this.game )

this.game
my.initial.pick

my.final.pick <- change_door( stay=T, opened.door=opened.door, a.pick=my.initial.pick )
determine_winner( final.pick=my.final.pick, game=this.game )

my.final.pick <- change_door( stay=F, opened.door=opened.door, a.pick=my.initial.pick )
determine_winner( final.pick=my.final.pick, game=this.game )

Any input on what's going wrong and suggestions for how to fix would be greatly appreciated.

Thanks!

Dselby86 commented 2 years ago

The logical expression needs to be inside the subset brackets

So instead of : game[ my.final.pick ] == "car" )

  return( "WIN" )

Use: game[ my.final.pick == "car" ] )

  return( "WIN" )

On Mon, Jan 17, 2022, 10:28 AM ellihammons21 @.***> wrote:

Hi everyone!

I'm working through Lab 01, and everything seems to work fine until Step

  1. This section of the code is taken directly from the instructions, and I'm fairly certain that I've set up the win/lose function correctly, so I'm not sure what's going wrong.

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

Then when I test this function, as shown below, I get the following error code:

the condition has length > 1 and only the first element will be usedError in determine_winner(final.pick = my.final.pick, game = this.game) : unused argument (final.pick = my.final.pick)

Specifically, it is throwing the error on this line of code: determine_winner( final.pick=my.final.pick, game=this.game )

this.game my.initial.pick

my.final.pick <- change_door( stay=T, opened.door=opened.door, a.pick=my.initial.pick ) determine_winner( final.pick=my.final.pick, game=this.game )

my.final.pick <- change_door( stay=F, opened.door=opened.door, a.pick=my.initial.pick ) determine_winner( final.pick=my.final.pick, game=this.game )

Any input on what's going wrong and suggestions for how to fix would be greatly appreciated.

Thanks!

— Reply to this email directly, view it on GitHub https://github.com/Watts-College/cpp-527-spr-2022/issues/9, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4EHB52VWS7IALXUPOIN7LUWRGTDANCNFSM5MFHHSTQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

ellihammons21 commented 2 years ago

Ok, I tried that as shown below, and now I'm getting this error:

Error in if (game[final.pick == "car"]) { : argument is of length zero


determine_winner <- function(my.final.pick, game )

{

   if( game[ my.final.pick == "car" ] )
   {
      return( "WIN" )
   }
   if( game[ my.final.pick == "goat" ] )
   {
      return( "LOSE" )
   }

}
lecy commented 2 years ago

Elli - the challenge is that you did not provide a reproducible example in your question.

this.game        # we need to know these values
my.initial.pick  # we need to know these values

my.final.pick <- change_door( stay=T, opened.door=opened.door, a.pick=my.initial.pick )
determine_winner( final.pick=my.final.pick, game=this.game )

my.final.pick <- change_door( stay=F, opened.door=opened.door, a.pick=my.initial.pick )
determine_winner( final.pick=my.final.pick, game=this.game )

To provide a reproducible example you either need to supply values for the inputs, or print the actual values you are passing:

# supplied values
this.game  <- c( "goat","car","goat" )
my.initial.pick  <- 2

# printed values copied from console
> this.game  
[1] "goat" "car" "goat"
> my.initial.pick 
[1] 2

You are getting a warning message (not an error, but it's a trivial difference here) at the determine_winner() stage here. But the problem is occurring in the code that you did not provide - the earlier code that produces the inputs into determine_winner(). As a result, it is hard to diagnose the issue. It's not easy, but try hard to provide reproducible examples (that's actually one of the skills you learn from participating in these discussion boards).

The problem in your case is that change_door() is not returning a single door that represents your final pick. It is returning two or more doors in some instances. I can replicate your warning message with the following:

game <- c( "goat","car","goat" )

###
###  WORKS FINE
###

my.final.pick <- 1
game[ my.final.pick ] == "car"
# [1] FALSE

if( game[ my.final.pick ] == "car"  )
{
    print( "WIN" )  # use print instead of return to test outside a function
}

# ...false, so no value printed...

my.final.pick <- 2
game[ my.final.pick ] == "car"
# [1] TRUE

if( game[ my.final.pick ] == "car"  )
{
    print( "WIN" )   
}
# [1] "WIN"

###
###  WARNING MESSAGE 
###

my.final.pick <- c( 1, 2 )
game[ my.final.pick ] == "car"
# [1]  FALSE TRUE

if( game[ my.final.pick ] == "car"  )
{
    print( "WIN" )  
}

# Warning message:
# In if (game[my.final.pick] == "car") { :
#   the condition has length > 1 and only the first element will be used
#
# ...false, so no value printed...

my.final.pick <- c( 2, 1 )  # change order 
game[ my.final.pick ] == "car"
# [1]  TRUE FALSE

if( game[ my.final.pick ] == "car"  )
{
    print( "WIN" )  
}

# Warning message:
# In if (game[my.final.pick] == "car") { :
#   the condition has length > 1 and only the first element will be used
#
# [1] "WIN"

only the first element will be used

game[ my.final.pick ] == "car"
# [1]  TRUE FALSE

Go back and review your change_door() and see if you can figure out what issue is causing the function to return multiple doors instead of a single final.pick.

ellihammons21 commented 2 years ago

Thank you! I'm still not seeing what could have gone wrong with the change door(). I'll post this section of the code below for you to review. Please let me know if you'd like me to email the rmd file, since I cant attach it here.

goat.door <- open_goat_door( this.game, my.initial.pick )

change_door <- function( stay=T, opened.door, a.pick )
{
  strategy <- c("stay","switch")

  final.pick <- sample(x=c(1:3)[-goat.door], size=1, replace=F )

  strategy.pick <- sample(strategy,size=1)

  if ( stay ){ final.pick <- my.initial.pick }

  else{ final.pick <- doors [-c(final.pick != my.initial.pick & final.pick != open_goat_door ( this.game, my.initial.pick ))] }

return (final.pick)
}
lecy commented 2 years ago

In algebra and programming you have universal containers called variables, and specific instances called values.

The container can take on ANY value. The instance assumes a SPECIFIC value.

y = 2*x + 5    # X is a container that can take on any value
x <- 3         # x is now the specific value 3

opened.door   # generic placeholder that can be any of the doors 
goat.door <- open_goat_door( this.game, my.initial.pick )   # is now a specific door in this game

When you create a new function you are defining the variables it needs to run.

When you run the function you assign specific instances using arguments and values.

You are mixing these two things up by references an INSTANCE instead of the generic VARIABLE NAME inside your function:

change_door <- function( stay=T, opened.door, a.pick )
{
  ... 
  sample(x=c(1:3)[ - goat.door ], size=1, replace=F )
}

### SHOULD BE: 

change_door <- function( stay=T, opened.door, a.pick )
{
  ... 
  sample(x=c(1:3)[ - opened.door ], size=1, replace=F )
}

# PASSING VALUES TO THE VARIABLES
change_door( stay=TRUE, opened.door=goat.door, a.pick=my.pick )

You also tell the function your strategy with the argument stay=TRUE or stay=FALSE. You don't randomly select it inside the function.

strategy.pick <- sample(strategy,size=1)

You want to be able to run experiments where you adopt one strategy and observe how well it works, then change the strategy and observe how well it works. You can't do that if you are randomly assigning strategies inside of the function.