[R] Issues when setting k to 1L
Paul Bernal
p@u|bern@|07 @end|ng |rom gm@||@com
Mon Apr 25 02:56:40 CEST 2022
Dear Rui,
I made the modification to k <- 1L (see the code below), but I get the
following odd result (maybe I am forgetting to do something):
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"
>
#single 1,000,000 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 <- 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)
print(paste("The average probability of success when doing 1,000,000 single
trials is:", average_prob_frame_6))
[[alternative HTML version deleted]]
More information about the R-help
mailing list