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, 2022 1,737 views

## 1 answer to this question.

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))))))`

Elevate Your Expertise with Our Machine Learning Certification Program!

• 5,480 points

## Is there a way to force the coefficient of the independent variable to be a positive coefficient in the linear regression model used in R?

A Few Constraints This is an example of ...READ MORE

## Role of the bias in neural networks.

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

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

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

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

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

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

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

## R: Calculate and interpret odds ratio in logistic regression

A logit, or the log of the ...READ MORE

## 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

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

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