[R] Issues when setting k to 1L

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Mon Apr 25 03:45:49 CEST 2022


Try: average_prob_frame_6 <- rowMeans(prob_frame_6, na.rm=TRUE)
Any NA propagates into calculations to make it NA.
Then again, should there be any NA?



-----Original Message-----
From: Paul Bernal <paulbernal07 using gmail.com>
To: Rui Barradas <ruipbarradas using sapo.pt>; R <r-help using r-project.org>
Sent: Sun, Apr 24, 2022 8:56 pm
Subject: [R] Issues when setting k to 1L

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]]

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list