Calculate the probability in R for sum of two dice rolls

0 votes

I have a matrix consist of the sum of dice 1 and dice 2 OR Y = D1 + D2

outer(1:6, 1:6, "+")

The output is:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    2    3    4    5    6    7
[2,]    3    4    5    6    7    8
[3,]    4    5    6    7    8    9
[4,]    5    6    7    8    9   10
[5,]    6    7    8    9   10   11
[6,]    7    8    9   10   11   12

How do I put it in a data frame called Y with the sum and the probability of each sum, for example:

Sum     pSum
2       1/36
3       2/36
4       3/36
.
.
. 
12      1/36

When I calculate for sum(Y$pSum), it will equal to 1

Apr 4 in Machine Learning by Dev
• 6,000 points
103 views

1 answer to this question.

0 votes

By converting the outer values to a vector ('Sum') and the proportion (prop.table) of frequencies (table) to 'pSum,' we may generate a data.frame.

v1 <- c(m1)
data.frame(Sum = v1, pSum =  as.numeric(prop.table(table(v1))[as.character(v1)]))

Output:

Sum       pSum
1    2 0.02777778
2    3 0.05555556
3    4 0.08333333
4    5 0.11111111
5    6 0.13888889
6    7 0.16666667
7    3 0.05555556
8    4 0.08333333
9    5 0.11111111
10   6 0.13888889
11   7 0.16666667
12   8 0.13888889
13   4 0.08333333
14   5 0.11111111
15   6 0.13888889
16   7 0.16666667
17   8 0.13888889
18   9 0.11111111
19   5 0.11111111
20   6 0.13888889
21   7 0.16666667
22   8 0.13888889
23   9 0.11111111
24  10 0.08333333
25   6 0.13888889
26   7 0.16666667
27   8 0.13888889
28   9 0.11111111
29  10 0.08333333
30  11 0.05555556
31   7 0.16666667
32   8 0.13888889
33   9 0.11111111
34  10 0.08333333
35  11 0.05555556
36  12 0.02777778

To get the summarized output, as the above output is too big.

stack(prop.table(table(v1)))[2:1]
#    ind     values
#1    2 0.02777778
#2    3 0.05555556
#3    4 0.08333333
#4    5 0.11111111
#5    6 0.13888889
#6    7 0.16666667
#7    8 0.13888889
#9   10 0.08333333
#10  11 0.05555556
#11  12 0.02777778

in this case;

m1 <- outer(1:6, 1:6, FUN = `+`)

an alternative is use expand.grid

stack(prop.table(table(do.call(`+`, expand.grid(rep(list(1:6), 2))))))

answered Apr 4 by Nandini
• 5,480 points

Related Questions In Machine Learning

0 votes
1 answer

Role of the bias in neural networks.

Hi@akhtar, The activation function in Neural Networks takes ...READ MORE

answered Jul 15, 2020 in Machine Learning by MD
• 95,360 points
699 views
0 votes
1 answer

How can I train a model and calculate the accuracy of CBR algorithm?

Hi@Abubakar, You can find lots of documents on ...READ MORE

answered Oct 17, 2020 in Machine Learning by MD
• 95,360 points
228 views
0 votes
1 answer

Empirical probability in R with x1+x2>2*x3

It's easy to duplicate random draws with ...READ MORE

answered Mar 15 in Machine Learning by Dev
• 6,000 points
39 views
0 votes
1 answer

Formula to calculate chance (probability) of a dice side based on its value

If I understand you correctly, you're looking ...READ MORE

answered Mar 17 in Machine Learning by Dev
• 6,000 points
33 views
0 votes
1 answer
0 votes
1 answer

Calculate Z-Score from Probability Value - R programming

It's named qnorm qnorm(p=0.841344746068543) Output 1 The following family of functions ...READ MORE

answered Apr 4 in Machine Learning by Nandini
• 5,480 points
97 views
0 votes
1 answer

Understanding the probability of a double-six if i roll two dice

The chance of not getting a double ...READ MORE

answered Mar 23 in Machine Learning by Nandini
• 5,480 points
356 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP