Open Jana-Ajeeb opened 2 years ago
Game is character vector and pick is a number.
You are mixing types in your logical statement.
Same problem and I'm trying to keep the type as numeric
# step 1: create a vector of doors as the game
# step 2: condition on doors not to include the picked choice or "cars"
# step 3: return the selection
open_goat_door <- function( game, a.pick )
{
doors <- as.numeric(game)
doors <- doors[doors!=a.pick && doors!=as.numeric("cars")]
opened.door <- sample(doors, size=1, replace=F)
return(opened.door) # number between 1 and 3
}
this.game <- create_game()
this.game
my.initial.pick <- select_door()
my.initial.pick
open_goat_door( this.game, my.initial.pick )
[1] "goat" "goat" "car" [1] 1 Warning in open_goat_door(this.game, my.initial.pick) : NAs introduced by coercion Warning in open_goat_door(this.game, my.initial.pick) : NAs introduced by coercion [1] NA
@Jana-Ajeeb Thank you for formatting your code!
The most important piece of advice I can offer this term and moving forward as a data programmer is to learn how to break open black boxes. It's the only way you can really understand problems.
# step 1: create a vector of doors as the game
# step 2: condition on doors not to include the picked choice or "cars"
# step 3: return the selection
open_goat_door <- function( game, a.pick )
{
doors <- game
doors <- doors[doors!=as.character(a.pick) & doors!="cars"]
opened.door <- sample(doors, size=1, replace=F)
return(as.numeric(opened.door)) # number between 1 and 3
}
# this.game <- create_game()
# my.initial.pick <- select_door()
# open_goat_door( this.game, my.initial.pick )
# ARGUMENT SECTION OF FUNCTION DOES THIS:
# game = this.game
# a.pick = my.initial.pick
# USE EXPLICIT VALUES FOR TESTING
game <- c("car","goat","goat")
a.pick <- 2
doors <- c(1,2,3)
# TEST EACH PART OF THE COMPOUND STATEMENT SEPARATELY
# doors[ doors != as.character(a.pick) & doors!="cars" ]
doors != a.pick
[1] TRUE FALSE TRUE
doors != "cars"
[1] TRUE TRUE TRUE
# criteria should not return TRUE for all cases!
Your problem is that you are conflating the door vector with the game vector.
The door is what the contestant sees - just the numbers:
doors <- c(1,2,3)
The game is what the contestant (or game show engineers) know - what is actually behind the doors.
game <- c("goat","car","goat")
The contestant tells you a DOOR NUMBER because it would be a lot easier if they can just say "door with the car please".
So to manipulate doors we need to use information from contestants and from game show hosts:
doors != a.pick & game != "goat"
Does that make sense?
yes that makes sense now thank you!!
This is where the NAs come from:
game <- c("goat","car","goat")
as.numeric(game)
You can't cast letters as numbers because there is no obvious translation.
Yess deal, thanks a lot😊
Hi, there! This thread has been helpful! I think I've gotten a little closer, but I'm still having problems. The function will still sometimes select a car or the initial pick... I don't see why this is happening, because of the subset I made.
I'm wondering if there's more I need to do to associate "doors" with the game and how I would go about doing that. Even then, that doesn't explain why the initial pick would be selected.
Let me know if you need any clarification for what I'm trying to think through.
#pseudo-code function 3
# set up game and door vectors
# make subset - must not be a car... or must be a goat AND must not be current selection
# set up return vector
# make a numerical selection
#function 3
open_goat_door <- function( game, a.pick )
{
doors <- c(1,2,3)
game <- c("goat", "goat", "cars")
doors <- doors[doors != a.pick & game != "car"]
opened.door <- sample(x=doors, size=1, replace=F)
return(as.numeric(opened.door)) # number between 1 and 3
}
@ctmccull Your solution looks great. The logic is intact and syntax looks good.
You are running into a very subtle error that I will talk about more in the solutions if you don't identify it before then. If you can't find it don't worry about it, but it's good that you at least identified that a bug exists!
You shouldn't need to recast your opened doors. The sample() function will retain the proper data type.
x <- sample( 1:10, 3 )
class( x )
[1] "integer"
y <- sample( as.character(1:10), 3 )
class(y)
[1] "character"
# return( as.numeric(opened.door) )
return( opened.door )
Hello, I'm trying to solve this step with the following logic but it's not working. I don't know where's the exact problem.
STEP 3 - HOST OPENS GOAT DOOR
Outcome: [1] "goat" "goat" "car" [1] 3 Warning in open_goat_door(this.game, my.initial.pick) : NAs introduced by coercion [1] NA