If I understand you correctly, it's simple.
Matrices in R are vectors with a dimension attribute. By default, they
are populated column by column. Use 'byrow = TRUE to populate by row
instead. For example:
> matrix (1:36, ncol = 12, byrow = TRUE)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
[2,] 13 14 15 16 17 18 19 20 21 22 23 24
[3,] 25 26 27 28 29 30 31 32 33 34 35 36
I leave it to you to use the 'dimnames' argument of ?matrix to give
names to the column and then subsequently convert to a data frame if
you like.
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Wed, Apr 20, 2022 at 8:38 PM Paul Bernal <paulbernal07 at gmail.com>
wrote:>
> 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]]
>
> ______________________________________________
> R-help at 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.