Home > OS >  Counting outcomes in Histogram
Counting outcomes in Histogram

Time:10-14

I have a code simulating N throws of a dice and I have managed to plot in a histogram with ggplot2. This is the code:

        N <- 10000
        Dice_Throws <- function(N) {
          Collected_Result <- sample (x = 1:6, size = N, replace = TRUE)
          return(Collected_Result)
        } 
        
        vec <- sample (x = 1:6, size = N, replace = TRUE)
        df <- data.frame(Dice = vec)
        
        ggplot(df, aes(Dice)) 
          geom_histogram(bins = 30)

If I now want to count the outcome for a certain number on the dice, how can I go about that? I just recently started learning and I've tried searching around a bit but just can't understand it.

Thank you kindly for any help.

CodePudding user response:

You can create a table to get the frequency of each outcome then subset it.

outcomes <- table(df$Dice)

 1    2    3    4    5    6 
1673 1744 1646 1581 1700 1656 

Then to get a specific value for an outcome e.g. 1 you can do this

outcomes[names(outcomes)==1]

1 
1673 
  • Related