ls() create_game <- function( ) { a.game <- sample( x=c("goat","goat","car"), size=3, replace=F ) return( a.game ) } ls() create_game create_game() create_game() create_game() create_game() create_game() create_game() args( sample ) select_door <- function( ) { doors <- c(1,2,3) a.pick <- sample( doors, size=1 ) return( a.pick ) # number between 1 and 3 } ls() class( "select_door" ) class( select_door ) select_door() select_door() select_door() select_door() select_door() select_door() select_door() select_door() select_door() select_door() select_door() select_door() select_door() ls() this.game <- create_game() this.game ls() class( this.game ) this.game my.initial.pick <- select_door() my.initial.pick dput( this.game ) ls() create_game() sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) game == "car" game <- c("goat", "car", "goat") game == "car" game == "car" game game = "car" game ! ( game == "car" ) game <- c("goat", "car", "goat") ! ( game == "car" ) game == "car" ! ( game == "car" ) game != "car" game a.pick ls() my.initial.pick a.pick <- 1 game != a.pick game a.pick class(game) class(a.pick) doors <- c(1,2,3) doors != a.pick criteria.2 <- doors != a.pick criteria.1 <- game != "car" criteria.1 criteria.2 criteria.1 & criteria.2 criteria.1 | criteria.2 # OR criteria.1 & criteria.2 criteria.1 & criteria.2 # AND criteria.1 | criteria.2 # OR criteria.1 & criteria.2 potential.opened.door <- criteria.1 & criteria.2 potential.opened.door game[ potential.opened.door ] doors[ potential.opened.door ] a.pick a.pick <- 2 potential.opened.door <- criteria.1 & criteria.2 potential.opened.door doors[ potential.opened.door ] both.criteria <- criteria.1 & criteria.2 potential.opened.doors <- doors[ potential.opened.door ] potential.opened.doors a.pick <- 2 criteria.1 <- game != "car" criteria.2 <- doors != a.pick both.criteria <- criteria.1 & criteria.2 both.criteria potential.opened.doors <- doors[ potential.opened.door ] potential.opened.doors potential.opened.doors <- doors[ both.criteria ] potential.opened.doors opened.door <- sample( potential.opened.doors, size=1 ) open_goat_door <- function( game, a.pick ) { doors <- c(1,2,3) # can't open door with car criteria.1 <- game != "car" # can't open the door that is currently selected criteria.2 <- doors != a.pick both.criteria <- criteria.1 & criteria.2 potential.opened.doors <- doors[ both.criteria ] opened.door <- sample( potential.opened.doors, size=1 ) return( opened.door ) # number between 1 and 3 } open_goat_door() this.game <- create_game() this.game my.initial.pick <- select_door() my.initial.pick open_goat_door( this.game, my.initial.pick ) this.game <- create_game() this.game my.initial.pick <- select_door() my.initial.pick open_goat_door( this.game, my.initial.pick ) this.game <- create_game() this.game my.initial.pick <- select_door() my.initial.pick open_goat_door( this.game, my.initial.pick ) this.game <- create_game() this.game my.initial.pick <- select_door() my.initial.pick open_goat_door( this.game, my.initial.pick ) help( sample ) sample( 1, size=1 ) sample( 1, size=1 ) sample( c("goat"), size=1 ) sample( c("goat"), size=1 ) sample( c("goat"), size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) sample( c(1,2,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,3), size=1 ) sample( c(1,2), size=1 ) sample( c(1,2), size=1 ) sample( c(1,2), size=1 ) sample( c(1,2), size=1 ) sample( c(1,2), size=1 ) sample( c(1,2), size=1 ) sample( c(1,2), size=1 ) sample( c(1,2), size=1 ) sample( c(2,3), size=1 ) sample( c(2,3), size=1 ) sample( c(2,3), size=1 ) sample( c(2,3), size=1 ) sample( c(2,3), size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) sample( 3, size=1 ) open_goat_door <- function( game, a.pick ) { doors <- c(1,2,3) # can't open door with car criteria.1 <- game != "car" # can't open the door that is currently selected criteria.2 <- doors != a.pick both.criteria <- criteria.1 & criteria.2 potential.opened.doors <- doors[ both.criteria ] if( length(potential.opened.doors) >= 2 ) { opened.door <- sample( potential.opened.doors, size=1 ) } if( length(potential.opened.doors) == 1 ) { opened.door <- potential.opened.doors } 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 ) open_goat_door( this.game, my.initial.pick ) open_goat_door( this.game, my.initial.pick ) open_goat_door( this.game, my.initial.pick ) open_goat_door( this.game, my.initial.pick ) open_goat_door( this.game, my.initial.pick ) open_goat_door( this.game, my.initial.pick ) ### UNIT TEST this.game <- c("goat","car","goat") my.initial.pick <- 1 # answer should be 3 opened.door <- open_goat_door( this.game, my.initial.pick ) opened.door == 3 this.game <- c("goat","car","goat") my.initial.pick <- 2 # answer should be 1 or 3 opened.door <- open_goat_door( this.game, my.initial.pick ) opened.door %in% c(1,3) this.game <- c("goat","car","goat") my.initial.pick <- 3 # answer should be 1 opened.door <- open_goat_door( this.game, my.initial.pick ) opened.door == 1 return( a.pick ) game <- this.game a.pick <- my.initial.pick doors <- c(1,2,3) # can't open door with car criteria.1 <- game != "car" criteria.1 criteria.2 <- doors != a.pick criteria.2 game game[ c(F,F,T) ] game[ c(T,F,T) ] game[ c(F,T,T) ] game[ c(1,3) ] game[ 2 ] game[ c(1,2) ] game[ 3 ] game[ 3 ] == "car" game[ 2 ] == "car"