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
We can create a data.frame
by converting the outer
values to a vector
('Sum') and the proportion (prop.table
) of frequencies (table
) as 'pSum'
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
If we just want the summarised output
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
where
m1 <- outer(1:6, 1:6, FUN = `+`)
Or another option is expand.grid
stack(prop.table(table(do.call(`+`, expand.grid(rep(list(1:6), 2))))))