Watts-College / cpp-527-spr-2022

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

Lab 02 - Warning from Lab 01 Solution Code #16

Open ellihammons21 opened 2 years ago

ellihammons21 commented 2 years ago

Hello,

I'm having a problem with the code for Part 1 of the Lab 2 Challenge Questions. Using the code from the Lab 2 solutions, I get the following warning message.

longer object length is not a multiple of shorter object length

This becomes a problem and slows things down signifcantly when I run the simulation. And input as to what is going wrong here would be appreciated!

build_doors <- function( n=5 ){ return( 1:n ) }

create_game <- function( )
{
    a.game <- sample( x=rep( c("goat","car"), c(3,2) ), size=5, replace=F )
    return( a.game )
}

select_door <- function( )
{
  doors <- build_doors() 
  a.pick <- sample( doors, size=1 )
  return( a.pick )  # number between 1 and 5
}

open_doors <- function( game, a.pick )
{
   # reveal one car and one goat

   doors <- build_doors()

   if( game[ a.pick ] == "car" )
   { 
     opened.car.door <- doors[ game == "car" & doors != a.pick ]
     goat.doors <- doors[ game != "car" ] 
     opened.goat.door <- sample( goat.doors, size=1 )
     opened.doors <- c( opened.car.door, opened.goat.door )
   }

   if( game[ a.pick ] == "goat" )
   { 
     opened.car.door <- sample( doors[game=="car"], size=1 )
     available.goat.doors <- doors[ game != "car" & doors != a.pick ] 
     opened.goat.door <- sample( available.goat.doors, size=1 )
     opened.doors <- c( opened.car.door, opened.goat.door )
   }
   return( opened.doors ) # two numbers
}

change_door <- function( stay=T, opened.doors, a.pick )
{
   doors <- build_doors()

   if( stay )
   {
     final.pick <- a.pick
   }
   if( ! stay )
   {
     available.doors <- doors[ ! ( doors %in% opened.doors | doors == a.pick ) ]
     final.pick  <- sample( available.doors, size=1 ) 
   }

   return( final.pick )  # number between 1 and 5
}

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

play_game <- function( )
{
  new.game <- create_game()
  first.pick <- select_door()
  opened.door <- open_goat_door( new.game, first.pick )

  final.pick.stay <- change_door( stay=T, opened.door, first.pick )
  final.pick.switch <- change_door( stay=F, opened.door, first.pick )

  outcome.stay <- determine_winner( final.pick.stay, new.game  )
  outcome.switch <- determine_winner( final.pick.switch, new.game )

  strategy <- c("stay","switch")
  outcome <- c(outcome.stay,outcome.switch)
  game.results <- data.frame( strategy, outcome,
                              stringsAsFactors=F )
  return( game.results )
}

play_game()
Dselby86 commented 2 years ago

When are you getting this error message? It is after play_game is called?

If it is in play_game, can you please run each of the play_game lines individually? Then we can see where the error occurred?

Thanks

On Mon, Jan 24, 2022 at 11:23 AM ellihammons21 @.***> wrote:

Hello,

I'm having a problem with the code for Part 1 of the Lab 2 Challenge Questions. Using the code from the Lab 2 solutions, I get the following warning message.

longer object length is not a multiple of shorter object length

This becomes a problem and slows things down signifcantly when I run the simulation. And input as to what is going wrong here would be appreciated!

build_doors <- function( n=5 ){ return( 1:n ) }

create_game <- function( ) { a.game <- sample( x=rep( c("goat","car"), c(3,2) ), size=5, replace=F ) return( a.game ) }

select_door <- function( ) { doors <- build_doors() a.pick <- sample( doors, size=1 ) return( a.pick ) # number between 1 and 5 }

open_doors <- function( game, a.pick ) {

reveal one car and one goat

doors <- build_doors()

if( game[ a.pick ] == "car" ) { opened.car.door <- doors[ game == "car" & doors != a.pick ] goat.doors <- doors[ game != "car" ] opened.goat.door <- sample( goat.doors, size=1 ) opened.doors <- c( opened.car.door, opened.goat.door ) }

if( game[ a.pick ] == "goat" ) { opened.car.door <- sample( doors[game=="car"], size=1 ) available.goat.doors <- doors[ game != "car" & doors != a.pick ] opened.goat.door <- sample( available.goat.doors, size=1 ) opened.doors <- c( opened.car.door, opened.goat.door ) } return( opened.doors ) # two numbers }

change_door <- function( stay=T, opened.doors, a.pick ) { doors <- build_doors()

if( stay ) { final.pick <- a.pick } if( ! stay ) { available.doors <- doors[ ! ( doors %in% opened.doors | doors == a.pick ) ] final.pick <- sample( available.doors, size=1 ) }

return( final.pick ) # number between 1 and 5 }

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

play_game <- function( ) { new.game <- create_game() first.pick <- select_door() opened.door <- open_goat_door( new.game, first.pick )

final.pick.stay <- change_door( stay=T, opened.door, first.pick ) final.pick.switch <- change_door( stay=F, opened.door, first.pick )

outcome.stay <- determine_winner( final.pick.stay, new.game ) outcome.switch <- determine_winner( final.pick.switch, new.game )

strategy <- c("stay","switch") outcome <- c(outcome.stay,outcome.switch) game.results <- data.frame( strategy, outcome, stringsAsFactors=F ) return( game.results ) }

play_game()

— Reply to this email directly, view it on GitHub https://github.com/Watts-College/cpp-527-spr-2022/issues/16, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4EHB4JWKGPYQWU3MQIS7TUXWKL3ANCNFSM5MWD22KA . 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

I think it's only happening after adding the game to a loop.

Dselby86 commented 2 years ago

Can you please post your Loop code?

On Mon, Jan 24, 2022 at 11:57 AM ellihammons21 @.***> wrote:

I think it's only happening after adding the game to a loop.

— Reply to this email directly, view it on GitHub https://github.com/Watts-College/cpp-527-spr-2022/issues/16#issuecomment-1020435356, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4EHB2AMHQ3DBQHJIXCUCDUXWOK3ANCNFSM5MWD22KA . 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 commented.Message ID: @.***>

ellihammons21 commented 2 years ago

I was able to fix it, thank you!!!

I'm also having a weird problem knitting my RMarkdown file. Where I try to knit, I get a pop-up saying that "Rendering R Markdown documents requires an updated version of the xfun package." When I click yes to install the package, I get the following message and am still never able to knit.

Show in New Window Warning message: package ‘sp’ was built under R version 4.0.3 R Console strategy

outcome stay LOSE switch WIN 2 rows data.frame 2 x 2 strategy outcome stay LOSE switch WIN 2 rows Show in New Window outcome strategy LOSE WIN stay 0.6637 0.3363 switch 0.3363 0.6637 Show in New Window strategy outcome stay WIN switch WIN 2 rows Show in New Window outcome strategy LOSE WIN stay 0.6004 0.3996 switch 0.4383 0.5617 Show in New Window strategy outcome stay WIN switch LOSE 2 rows Show in New Window outcome strategy LOSE WIN stay 0.7025 0.2975 switch 0.6366 0.3634 Show in New Window Warning in game != "car" & doors != a.pick : longer object length is not a multiple of shorter object length R Console strategy outcome stay LOSE switch LOSE 2 rows data.frame 2 x 2 strategy outcome stay LOSE switch LOSE 2 rows Show in New Window outcome strategy LOSE WIN stay 0.8048 0.1952 switch 0.7479 0.2521 Show in New Window Warning in game != "car" & doors != a.pick : longer object length is not a multiple of shorter object length R Console strategy outcome stay LOSE switch LOSE 2 rows data.frame 2 x 2 strategy outcome stay LOSE switch LOSE 2 rows Show in New Window outcome strategy LOSE WIN stay 0.8991 0.1009 switch 0.8629 0.1371 Installing 'xfun' for R Markdown... Installing package into 'C:/Users/Bethany Hammons/Documents/R/win-library/4.0' (as 'lib' is unspecified) trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/xfun_0.29.zip' Content type 'application/zip' length 403742 bytes (394 KB) ================================================== downloaded 394 KB package 'xfun' successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package 'xfun' The downloaded binary packages are in C:\Users\Bethany Hammons\AppData\Local\Temp\RtmpAt4rz1\downloaded_packages Warning: restored 'xfun' Package 'xfun' successfully installed. Warning message: In file.copy(savedcopy, lib, recursive = TRUE) : problem copying C:\Users\Bethany Hammons\Documents\R\win-library\4.0\00LOCK\xfun\libs\x64\xfun.dll to C:\Users\Bethany Hammons\Documents\R\win-library\4.0\xfun\libs\x64\xfun.dll: Permission denied Any suggestions on how to fix this?
Dselby86 commented 2 years ago

Try Updating R to version 4.1 and making sure Rstudio has its most recent version.

If that doesn't work go to your C:\Users\Bethany Hammons\Documents\R\Documents\R\win-library\4.0 folder and erase the xfun folder.

On Mon, Jan 24, 2022 at 1:38 PM ellihammons21 @.***> wrote:

I was able to fix it, thank you!!!

I'm also having a weird problem knitting my RMarkdown file. Where I try to knit, I get a pop-up saying that "Rendering R Markdown documents requires an updated version of the xfun package." When I click yes to install the package, I get the following message and am still never able to knit.

Show in New Window Warning message: package ‘sp’ was built under R version 4.0.3 R Console strategy

outcome

stay LOSE switch WIN 2 rows data.frame 2 x 2 strategy

outcome

stay LOSE switch WIN 2 rows Show in New Window outcome strategy LOSE WIN stay 0.6637 0.3363 switch 0.3363 0.6637 Show in New Window strategy

outcome

stay WIN switch WIN 2 rows Show in New Window outcome strategy LOSE WIN stay 0.6004 0.3996 switch 0.4383 0.5617 Show in New Window strategy

outcome

stay WIN switch LOSE 2 rows Show in New Window outcome strategy LOSE WIN stay 0.7025 0.2975 switch 0.6366 0.3634 Show in New Window Warning in game != "car" & doors != a.pick : longer object length is not a multiple of shorter object length R Console strategy

outcome

stay LOSE switch LOSE 2 rows data.frame 2 x 2 strategy

outcome

stay LOSE switch LOSE 2 rows Show in New Window outcome strategy LOSE WIN stay 0.8048 0.1952 switch 0.7479 0.2521 Show in New Window Warning in game != "car" & doors != a.pick : longer object length is not a multiple of shorter object length R Console strategy

outcome

stay LOSE switch LOSE 2 rows data.frame 2 x 2 strategy

outcome

stay LOSE switch LOSE 2 rows Show in New Window outcome strategy LOSE WIN stay 0.8991 0.1009 switch 0.8629 0.1371 Installing 'xfun' for R Markdown... Installing package into 'C:/Users/Bethany Hammons/Documents/R/win-library/4.0' (as 'lib' is unspecified) trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/xfun_0.29.zip ' Content type 'application/zip' length 403742 bytes (394 KB)

downloaded 394 KB

package 'xfun' successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package 'xfun'

The downloaded binary packages are in C:\Users\Bethany Hammons\AppData\Local\Temp\RtmpAt4rz1\downloaded_packages Warning: restored 'xfun'

<U+2714> Package 'xfun' successfully installed. Warning message: In file.copy(savedcopy, lib, recursive = TRUE) : problem copying C:\Users\Bethany Hammons\Documents\R\win-library\4.0\00LOCK\xfun\libs\x64\xfun.dll to C:\Users\Bethany Hammons\Documents\R\win-library\4.0\xfun\libs\x64\xfun.dll: Permission denied

Any suggestions on how to fix this?

— Reply to this email directly, view it on GitHub https://github.com/Watts-College/cpp-527-spr-2022/issues/16#issuecomment-1020525894, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4EHBZB2GRV36CAZAWWPVLUXW2D5ANCNFSM5MWD22KA . 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 commented.Message ID: @.***>