# [R] R Code Execution taking forever

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Apr 25 09:41:15 CEST 2022

```Hello,

Yes, that's it.
Now the results dataframe prob_frame_6  is created with the right number
of columns, this number depends on the number of iterations K. Before it
would always have 10 columns.

In the example run below I have timed the code with K <- 20L and changed
the final print instruction a bit to make it more general purpose.

system.time({

options(width=205)

# these two are equal
cnames0 <-
c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
cnames <- month.abb
identical(cnames0, cnames)
# [1] TRUE

# performing 1,000,000 simulations 10 times
num_trials_6 <- 1e6
dice_rolls_6 <- num_trials_6*12
num_dice_6 <- 1
dice_sides_6 <- 6

set.seed(2022)

K <- 20L
prob_frame_6 <- as.data.frame(matrix(ncol = K, nrow = 1L))

for(k in seq_len(K)){
#
dice_simul_6 <- sample(dice_sides_6, dice_rolls_6, replace = TRUE)
# constructing matrix containing results of all dice rolls by month
prob_matrix_6 <- matrix(dice_simul_6, ncol = 12, byrow = TRUE)

# naming each column by it's corresponding month name
colnames(prob_matrix_6) <- month.abb

# calculating column  which will have a 1
# if trial was successful and a 0 otherwise
success <- integer(num_trials_6)
for(i in seq_len(num_trials_6)){
success[i] <- as.integer(all(1:6 %in% prob_matrix_6[i, ]))
}

#calculating probability of success

p6 <- mean(success)
prob_frame_6[1, k] <- p6
}

colnames(prob_frame_6) <- sprintf("p%d", seq_len(K))
average_prob_frame_6 <- rowMeans(prob_frame_6)
final_frame_6 <- cbind(prob_frame_6, average_prob_frame_6)

write.csv(final_frame_6, "OneMillion_Trials_Ten_Times_Results.csv")

print(final_frame_6)
fmt <- "The average probability of success when doing %s trials %d
times is: %0.5g"
txt <- sprintf(fmt, formatC(num_trials_6, format = "fg", big.mark =
","), K, average_prob_frame_6)
print(txt)
})
#>         p1       p2       p3       p4       p5       p6       p7
p8       p9      p10      p11      p12      p13      p14      p15
p16     p17      p18      p19      p20 average_prob_frame_6
#> 1 0.437738 0.438101 0.438051 0.437195 0.437623 0.437977 0.437828
0.437546 0.437663 0.438468 0.437435 0.438332 0.438678 0.438268 0.438047
0.438817 0.43817 0.437762 0.438403 0.437472            0.4379787
#> [1] "The average probability of success when doing 1,000,000 trials
20 times is: 0.43798"
#>    user  system elapsed
#>   62.25    0.44   62.71

Hope this helps,

Às 08:24 de 25/04/2022, Paul Bernal escreveu:
> Dear friend Rui,
>
> So, if I understood correctly, I should do this:
>
> K <- 1L
> prob_frame_6 <- as.data.frame(matrix(ncol = K, nrow = 1L))
>
>
> prob_frame_6 <- as.data.frame(matrix(ncol = 10L, nrow = 1L))
> K <- 1
>
> Right?
>
> Cheers,
> Paul
>
> Do I should do this:
>
> El El lun, 25 de abr. de 2022 a la(s) 2:11 a. m., Rui Barradas
>
>     Hello,
>
>     You forgot to cc the list.
>
>     The bug comes from creating a results data.frame with 10 columns when
>     there's only one iteration K. Swap these lines
>
>
>     prob_frame_6 <- as.data.frame(matrix(ncol = 10L, nrow = 1L))
>     K <- 1
>
>
>     and set ncol = K:
>
>
>     K <- 1L
>     prob_frame_6 <- as.data.frame(matrix(ncol = K, nrow = 1L))
>
>
>     You now have final_frame_6 with 2 columns, one from prob_frame_6 and
>     the
>     other its row mean.
>     Try also with K <- 2L or another number of iterations.
>
>
>     Hope this helps,
>
>
>
>     Às 01:49 de 25/04/2022, Paul Bernal escreveu:
>      > Dear Rui,
>      >
>      > Hope you are doing great.
>      >
>      > This is the code based on yours (I modified it to run just 1 time
>      > instead of 10 times):
>      >
>      > #performing 1,000,000 single trials
>      >
>      > # these two are equal
>      > cnames0 <-
>      >
>     c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
>      > cnames <- month.abb
>      > identical(cnames0, cnames)
>      > # [1] TRUE
>      >
>      > # performing 1,000,000 simulations 10 times
>      > num_trials_6 <- 1e6
>      > dice_rolls_6 <- num_trials_6*12
>      > num_dice_6 <- 1
>      > dice_sides_6 <- 6
>      >
>      > set.seed(2022)
>      >
>      > prob_frame_6 <- as.data.frame(matrix(ncol = 10L, nrow = 1L))
>      > K <- 1
>      > for(k in seq_len(K)){
>      >    #
>      >    dice_simul_6 <- sample(dice_sides_6, dice_rolls_6, replace = TRUE)
>      >    # constructing matrix containing results of all dice rolls by
>     month
>      >    prob_matrix_6 <- matrix(dice_simul_6, ncol = 12, byrow = TRUE)
>      >
>      >    # naming each column by it's corresponding month name
>      >    colnames(prob_matrix_6) <- month.abb
>      >
>      >    # calculating column  which will have a 1
>      >    # if trial was successful and a 0 otherwise
>      >    success <- integer(num_trials_6)
>      >    for(i in seq_len(num_trials_6)){
>      >      success[i] <- as.integer(all(1:6 %in% prob_matrix_6[i, ]))
>      >    }
>      >
>      >    #calculating probability of success
>      >
>      >    p6 <- mean(success)
>      >    prob_frame_6[1, k] <- p6
>      > }
>      >
>      > colnames(prob_frame_6) <- sprintf("p%d", seq_len(K))
>      > average_prob_frame_6 <- rowMeans(prob_frame_6)
>      > final_frame_6 <- cbind(prob_frame_6, average_prob_frame_6)
>      >
>      > write.csv(final_frame_6, "OneMillion_Trials_Ten_Times_Results.csv")
>      >
>      > print(final_frame_6)
>      > print(paste("The average probability of success when doing 1,000,000
>      > single trials is:", average_prob_frame_6))
>      >
>      > however, for some reason I get this result:
>      >
>      >  > print(final_frame_6)
>      >          p1 NA NA NA NA NA NA NA NA NA average_prob_frame_6
>      > 1 0.437738 NA NA NA NA NA NA NA NA NA                   NA
>      >  > print(paste("The average probability of success when doing
>     1,000,000
>      > single trials is:", average_prob_frame_6))
>      > [1] "The average probability of success when doing 1,000,000 single
>      > trials is: NA"
>      >
>      > Any idea of why this could be happening?
>      >
>      > Best,
>      > Paul
>      >
>      >
>      >
>      > El dom, 24 abr 2022 a las 9:38, Rui Barradas
>     escribió:
>      >
>      >     Hello,
>      >
>      >     Thanks for the link, the package is TeachingDemos, it's the
>     function
>      >     that's named dice. And the source code shows that it calls
>     sample()
>      >     in a
>      >     way similar to mine, so the code I posted should give
>     approximately the
>      >     same results.
>      >
>      >     To run just once, change to K <- 1L, right before the main
>     for loop.
>      >
>      >     Hope this helps,
>      >
>      >
>      >     Às 15:24 de 24/04/2022, Paul Bernal escreveu:
>      >      > Dear friend Rui,
>      >      >
>      >      > Thank you so much for your extremely valuable help.
>      >      >
>      >      > This is the dice function I used:
>      >      >
>      >
>     https://www.rdocumentation.org/packages/TeachingDemos/versions/2.12/topics/dice
>     <https://www.rdocumentation.org/packages/TeachingDemos/versions/2.12/topics/dice>
>      >
>       <https://www.rdocumentation.org/packages/TeachingDemos/versions/2.12/topics/dice <https://www.rdocumentation.org/packages/TeachingDemos/versions/2.12/topics/dice>>
>      >
>      >      >
>      >
>       <https://www.rdocumentation.org/packages/TeachingDemos/versions/2.12/topics/dice <https://www.rdocumentation.org/packages/TeachingDemos/versions/2.12/topics/dice>
>      >
>       <https://www.rdocumentation.org/packages/TeachingDemos/versions/2.12/topics/dice <https://www.rdocumentation.org/packages/TeachingDemos/versions/2.12/topics/dice>>>
>      >      >
>      >      > One question, how would I modify your code to run it for
>      >     1,000,000 rolls
>      >      > 1 time?
>      >      >
>      >      > Best,
>      >      > Paul
>      >      >
>      >      > El dom, 24 abr 2022 a las 8:58, Rui Barradas
>      >     escribió:
>      >      >
>      >      >     Hello,
>      >      >
>      >      >     I still can't find the package dice you are using,
>     it's not
>      >     the one on
>      >      >     CRAN, that one only has two functions, like I said
>     earlier.
>      >      >
>      >      >     Anyway, I have replaced function dice(9 by a call to
>     sample().
>      >      >     And simplified the code a lot. It takes half a minute
>     to run the
>      >      >     1,000,000 simulations K = 10 times (upper case K).
>      >      >     See if this is what you want.
>      >      >
>      >      >
>      >      >     # these two are equal
>      >      >     cnames0 <-
>      >      >
>      >
>       c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
>      >      >     cnames <- month.abb
>      >      >     identical(cnames0, cnames)
>      >      >     # [1] TRUE
>      >      >
>      >      >     # performing 1,000,000 simulations 10 times
>      >      >     num_trials_6 <- 1e6
>      >      >     dice_rolls_6 <- num_trials_6*12
>      >      >     num_dice_6 <- 1
>      >      >     dice_sides_6 <- 6
>      >      >
>      >      >     set.seed(2022)
>      >      >
>      >      >     prob_frame_6 <- as.data.frame(matrix(ncol = 10L, nrow
>     = 1L))
>      >      >     K <- 10L
>      >      >     for(k in seq_len(K)){
>      >      >         #
>      >      >         dice_simul_6 <- sample(dice_sides_6, dice_rolls_6,
>      >     replace = TRUE)
>      >      >         # constructing matrix containing results of all dice
>      >     rolls by month
>      >      >         prob_matrix_6 <- matrix(dice_simul_6, ncol = 12,
>     byrow =
>      >     TRUE)
>      >      >
>      >      >         # naming each column by it's corresponding month name
>      >      >         colnames(prob_matrix_6) <- month.abb
>      >      >
>      >      >         # calculating column  which will have a 1
>      >      >         # if trial was successful and a 0 otherwise
>      >      >         success <- integer(num_trials_6)
>      >      >         for(i in seq_len(num_trials_6)){
>      >      >           success[i] <- as.integer(all(1:6 %in%
>     prob_matrix_6[i, ]))
>      >      >         }
>      >      >
>      >      >         #calculating probability of success
>      >      >
>      >      >         p6 <- mean(success)
>      >      >         prob_frame_6[1, k] <- p6
>      >      >     }
>      >      >
>      >      >     colnames(prob_frame_6) <- sprintf("p%d", seq_len(K))
>      >      >     average_prob_frame_6 <- rowMeans(prob_frame_6)
>      >      >     final_frame_6 <- cbind(prob_frame_6, average_prob_frame_6)
>      >      >
>      >      >     write.csv(final_frame_6,
>      >     "OneMillion_Trials_Ten_Times_Results.csv")
>      >      >
>      >      >     print(final_frame_6)
>      >      >     print(paste("The average probability of success when doing
>      >     1,000,000
>      >      >     trials 10 times is:", average_prob_frame_6))
>      >      >
>      >      >
>      >      >     Hope this helps,
>      >      >
>      >      >
>      >      >     Às 12:14 de 24/04/2022, Paul Bernal escreveu:
>      >      >      > Dear Rui,
>      >      >      >
>      >      >      > There is a package called dice, that package is the
>     one I am
>      >      >     using. This
>      >      >      > package has a función called dice.
>      >      >      >
>      >      >      > Best,
>      >      >      >
>      >      >      > Paul
>      >      >      >
>      >      >      > El El dom, 24 de abr. de 2022 a la(s) 4:43 a. m.,
>      >      >     <mailto:ruipbarradas using sapo.pt
>      >      >      >
>      >      >      >     Hello,
>      >      >      >
>      >      >      >     I'm having trouble running the code, where does
>      >     function dice
>      >      >     come from?
>      >      >      >     CRAN package dice only has two functions,
>      >      >      >
>      >      >      >     getEventProb
>      >      >      >     getSumProbs
>      >      >      >
>      >      >      >     not a function dice.
>      >      >      >
>      >      >      >     Can you post a link to where the
>     package/function can
>      >     be found?
>      >      >      >
>      >      >      >     Rui Barradas
>      >      >      >
>      >      >      >
>      >      >      >     Às 02:00 de 24/04/2022, Paul Bernal escreveu:
>      >      >      >      > Dear R friends,
>      >      >      >      >
>      >      >      >      > Hope you are doing great. The reason why I am
>      >     contacting
>      >      >     you all, is
>      >      >      >      > because the code I am sharing with you takes
>      >     forever. It
>      >      >     started
>      >      >      >     running at
>      >      >      >      > 2:00 AM today, and it's 7:52 PM and is still
>      >     running (see
>      >      >     code at
>      >      >      >     the end
>      >      >      >      > of this mail).
>      >      >      >      >
>      >      >      >      > I am using Rx64  4.1.2, and the code is being
>      >     executed in
>      >      >      >     RStudio. The
>      >      >      >      > RStudio version I am currently using is Version
>      >     2022.02.0
>      >      >     Build 443
>      >      >      >      > "Prairie Trillium" Release (9f796939,
>     2022-02-16)
>      >     for Windows.
>      >      >      >      >
>      >      >      >      > My PC specs:
>      >      >      >      > Processor: Intel(R) Core(TM) i5-10310U CPU @
>     1.70 GHz
>      >      >      >      > Installed RAM: 16.0 GB (15.6 GB usable)
>      >      >      >      > System type: 64-bit operating system, x64-based
>      >     processor
>      >      >      >      > Local Disc(C:) Free Space: 274 GB
>      >      >      >      >
>      >      >      >      > I am wondering if there is/are a set of system
>      >     variable(s) or
>      >      >      >     something I
>      >      >      >      > could do to improve the performance of the
>     program.
>      >      >      >      >
>      >      >      >      > It is really odd this code has taken this
>     much (and
>      >     it is
>      >      >     still
>      >      >      >     running).
>      >      >      >      >
>      >      >      >      > Any help and/or guidance would be greatly
>     appreciated.
>      >      >      >      >
>      >      >      >      > Best regards,
>      >      >      >      > Paul
>      >      >      >      >
>      >      >      >      >
>      >      >      >      >
>      >      >      >      >
>      >      >      >      > #performing 1,000,000 simulations 10 times
>      >      >      >      > num_trials_6 = 1000000
>      >      >      >      > dice_rolls_6 = num_trials_6*12
>      >      >      >      > num_dice_6 = 1
>      >      >      >      > dice_sides_6 = 6
>      >      >      >      >
>      >      >      >      > prob_frame_6 <- data.frame(matrix(ncol = 10,
>     nrow = 1))
>      >      >      >      >
>      >      >      >      > k <- 0
>      >      >      >      > while(k < 10){
>      >      >      >      >    dice_simul_6 = data.frame(dice(rolls =
>     dice_rolls_6,
>      >      >     ndice =
>      >      >      >     num_dice_6,
>      >      >      >      > sides = dice_sides_6, plot.it
>     <http://plot.it> <http://plot.it <http://plot.it>>
>      >     <http://plot.it <http://plot.it> <http://plot.it
>     <http://plot.it>>>
>      >      >     <http://plot.it <http://plot.it> <http://plot.it
>     <http://plot.it>> <http://plot.it <http://plot.it>
>      >     <http://plot.it <http://plot.it>>>> = FALSE))
>      >      >      >      >
>      >      >      >      >    #constructing matrix containing results
>     of all dice
>      >      >     rolls by month
>      >      >      >      >    prob_matrix_6 <-
>      >     data.frame(matrix(dice_simul_6[,1], ncol =
>      >      >      >     12, byrow =
>      >      >      >      > TRUE))
>      >      >      >      >
>      >      >      >      >    #naming each column by it's corresponding
>     month name
>      >      >      >      >    colnames(prob_matrix_6) <-
>      >      >      >      >
>      >      >      >
>      >      >
>      >
>       c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
>      >      >      >      >
>      >      >      >      >
>      >      >      >      >    #assigning each person´s name depending
>     on the
>      >     number
>      >      >     showed
>      >      >      >     in the dice
>      >      >      >      > once rolled
>      >      >      >      >    for (i in 1:nrow(prob_matrix_6)){
>      >      >      >      >      for (j in 1:ncol(prob_matrix_6)){
>      >      >      >      >        if (prob_matrix_6[i,j] == 1){
>      >      >      >      >          prob_matrix_6[i,j] = "Alice"
>      >      >      >      >        }
>      >      >      >      >        if (prob_matrix_6[i,j] == 2){
>      >      >      >      >          prob_matrix_6[i,j] = "Bob"
>      >      >      >      >        }
>      >      >      >      >        if (prob_matrix_6[i,j] == 3){
>      >      >      >      >          prob_matrix_6[i,j] = "Charlie"
>      >      >      >      >        }
>      >      >      >      >        if (prob_matrix_6[i,j] == 4){
>      >      >      >      >          prob_matrix_6[i,j] = "Don"
>      >      >      >      >        }
>      >      >      >      >        if (prob_matrix_6[i,j] == 5){
>      >      >      >      >          prob_matrix_6[i,j] = "Ellen"
>      >      >      >      >        }
>      >      >      >      >        if (prob_matrix_6[i,j] == 6){
>      >      >      >      >          prob_matrix_6[i,j] = "Fred"
>      >      >      >      >        }
>      >      >      >      >
>      >      >      >      >      }
>      >      >      >      >    }
>      >      >      >      >
>      >      >      >      >    #calculating column  which will have a 1 if
>      >     trial was
>      >      >      >     successful and a 0
>      >      >      >      > otherwise
>      >      >      >      >    prob_matrix_6['success'] <- for (i in
>      >      >     1:nrow(prob_matrix_6)){
>      >      >      >      >      if (("Alice" %in% prob_matrix_6[i,]) &
>     ("Bob" %in%
>      >      >      >     prob_matrix_6[i,]) &
>      >      >      >      > ("Charlie" %in% prob_matrix_6[i,]) & ("Don" %in%
>      >      >     prob_matrix_6[i,]) &
>      >      >      >      > ("Ellen" %in% prob_matrix_6[i,]) & ("Fred" %in%
>      >      >     prob_matrix_6[i,])){
>      >      >      >      >        prob_matrix_6[i,13] = 1
>      >      >      >      >      }else{
>      >      >      >      >        prob_matrix_6[i,13] = 0
>      >      >      >      >      }
>      >      >      >      >    }
>      >      >      >      >
>      >      >      >      >    #relabeling column v13 so that its new
>     name is
>      >     success
>      >      >      >      >    colnames(prob_matrix_6)[13] <- "success"
>      >      >      >      >
>      >      >      >      >
>      >      >      >      >    #calculating probability of success
>      >      >      >      >
>      >      >      >      >    p6 =
>     sum(prob_matrix_6\$success)/nrow(prob_matrix_6)
>      >      >      >      >    prob_frame_6 <- cbind(prob_frame_6, p6)
>      >      >      >      >
>      >      >      >      >    k = k + 1
>      >      >      >      >
>      >      >      >      > }
>      >      >      >      >
>      >      >      >      > prob_frame_6 <- prob_frame_6[11:20]
>      >      >      >      > colnames(prob_frame_6) <-
>      >      >      >      >
>     c("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10")
>      >      >      >      > average_prob_frame_6 <- rowMeans(prob_frame_6)
>      >      >      >      > trial_1000000_10_frame <- cbind(prob_frame_6,
>      >      >     average_prob_frame_6)
>      >      >      >      > final_frame_6 <- trial_1000000_10_frame
>      >      >      >      > colnames(final_frame_6) <-
>      >      >      >      >
>     c("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10",
>      >      >      >     "avg_prob_frame_5")
>      >      >      >      >
>      >      >      >      > write.csv(final_frame_6,
>      >      >     "OneMillion_Trials_Ten_Times_Results.csv")
>      >      >      >      > print(final_frame_6)
>      >      >      >      > print(paste("The average probability of success
>      >     when doing
>      >      >      >     1,000,000 trials
>      >      >      >      > 10 times is:", average_prob_frame_6))
>      >      >      >      >
>      >      >      >      >       [[alternative HTML version deleted]]
>      >      >      >      >
>      >      >      >      > ______________________________________________
>      >      >      >      > R-help using r-project.org
>     <mailto:R-help using r-project.org> <mailto:R-help using r-project.org
>     <mailto:R-help using r-project.org>>
>      >     <mailto:R-help using r-project.org <mailto:R-help using r-project.org>
>     <mailto:R-help using r-project.org <mailto:R-help using r-project.org>>>
>      >      >     <mailto:R-help using r-project.org
>     <mailto:R-help using r-project.org> <mailto:R-help using r-project.org
>     <mailto:R-help using r-project.org>>
>      >     <mailto:R-help using r-project.org <mailto:R-help using r-project.org>
>     <mailto:R-help using r-project.org <mailto:R-help using r-project.org>>>> mailing
>      >     list
>      >      >      >     -- To UNSUBSCRIBE and more, see
>      >      >      >      > https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>      >     <https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>>
>      >      >     <https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>      >     <https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>>>
>      >      >      >     <https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>      >     <https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>>
>      >      >     <https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>      >     <https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>>>>
>      >      >      > http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>      >     <http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>>
>      >      >     <http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>      >     <http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>>>
>      >      >      >     <http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>      >     <http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>>
>      >      >     <http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>      >     <http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>>>>
>      >      >      >      > and provide commented, minimal, self-contained,
>      >      >     reproducible code.
>      >      >      >
>      >      >
>      >
>

```