[R] Model To Simulate Dice Roll
Paul Bernal
p@u|bern@|07 @end|ng |rom gm@||@com
Thu Apr 21 05:37:57 CEST 2022
Dear friends,
Hope you are doing well. I need to simulate a 1 dice roll for each one of
the twelve months of the year and perform 100 trials, so I thought of
constructing a dataframe with twelve columns and 100 rows the following way:
num_rows = 100
prob_frame <- data.frame(matrix(NA, nrow = num_rows, ncol = 12))
colnames(prob_frame)<-c("January","February","March","April","May","June","July","August","September","October","November","December")
Now, using the dice package, I can simulate n number of dice rolls as
follows:
#performing simulation
dice_simul = dice(rolls = dice_rolls, ndice = num_dice, sides = dice_sides,
plot.it = TRUE)
What I would like to do is to populate each column and row with the results
of dice_simul.
Let me show you the structure of dice_simul:
> str(dice_simul)
Classes ‘dice’ and 'data.frame': 100 obs. of 1 variable:
$ Red: int 2 2 1 2 5 4 4 6 1 4 ...
> dput(dice_simul)
structure(list(Red = c(2L, 2L, 1L, 2L, 5L, 4L, 4L, 6L, 1L, 4L,
4L, 2L, 6L, 2L, 2L, 1L, 3L, 6L, 1L, 5L, 5L, 5L, 3L, 4L, 2L, 6L,
4L, 6L, 6L, 2L, 1L, 2L, 2L, 6L, 4L, 2L, 3L, 5L, 6L, 6L, 4L, 5L,
4L, 6L, 6L, 3L, 4L, 1L, 5L, 3L, 3L, 5L, 3L, 4L, 1L, 3L, 3L, 2L,
4L, 1L, 2L, 1L, 6L, 3L, 5L, 5L, 3L, 4L, 4L, 5L, 4L, 1L, 5L, 3L,
4L, 4L, 3L, 6L, 5L, 2L, 4L, 1L, 1L, 6L, 4L, 3L, 6L, 5L, 6L, 2L,
6L, 1L, 6L, 6L, 4L, 3L, 4L, 2L, 1L, 5L)), class = c("dice", "data.frame"
), row.names = c(NA, -100L))
For example, the first number of dice_simul should go to row 1 for January,
the second number of dice_simul should go to row 1 for February, ... the
twelveth number of dice_simul should go to row 1 for December, the 13th
number should go to row 2 for january, and so on.
This is what I tried to do but doesn´t work they way I want to:
#1)dice_rolls which is the number of times the dice will be rolled
#2)num_dice which is the number of dice that will be rolled each time
#3)dice_sides which is the number of sides of the dice
#function dice will take each one of these variables as its parameter to
perform the simulation
dice_rolls = 100
num_dice = 1
dice_sides = 6
#performing simulation
dice_simul = dice(rolls = dice_rolls, ndice = num_dice, sides = dice_sides,
plot.it = TRUE)
num_rows = 100
prob_frame <- data.frame(matrix(NA, nrow = num_rows, ncol = 12))
colnames(prob_frame) <-
c("January","February","March","April","May","June","July","August","September","October","November","December")
for (j in 1:12){
for (i in 1:num_rows){
prob_frame[i,j]=dice_simul[i,1]
}
}
I basically want to populate the twelve months for the first row, then the
twelve months for the second row, and so on, until I get to populate the
twelve months for the last row sequentially.
How could I accomplish this?
Any help and/or guidance will be greatly appreciated.
Best regards,
Paul
[[alternative HTML version deleted]]
More information about the R-help
mailing list