Open kidistbetter105 opened 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" ) }
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
Ok it might be.
I got this Error from step 4, and am trying to look back to the previous codes.
Thanks!
yes, its step 4 , it keep saying that 'door 2' is not found. what do I have to change to fix the error?
our if() now it solved. Thanks Dr. lecy !
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 :-)
Make sure to paste code and not screen captures otherwise you are asking others to retype your function to test it.
Make sure to paste code and not screen captures otherwise you are asking others to retype your function to test it.
OK! Sorry about that !
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 )
What are these values, though?
this.game
my.initial.pick
my.final.pick
opened.door
Object values would look something like this:
> this.game
[1] "goat" "car" "goat"
> my.initial.pick
[1] 2
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"
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
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
# }
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" ]
You are very welcome!
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