# [R] Making a 'joint distribution'?

Ajay Shah ajayshah at mayin.org
Sun Oct 3 11:10:04 CEST 2004

```Suppose I make two discrete variables --
> D <- data.frame(f1=sample(1:5,100,replace=T), f2=sample(1:5,100,replace=T))

I know I can do:

> table(D\$f1, D\$f2)
0 1 2 3 4
0 5 5 5 5 4
1 4 2 6 7 3
2 5 3 5 3 6
3 3 1 3 1 2
4 6 4 3 3 6
> table(D\$f1)
0  1  2  3  4
24 22 22 10 22
> table(D\$f2)
0  1  2  3  4
23 15 22 19 21

which is all great. But how do I produce the typical presentation of
the "joint distribution" where we put the marginal distributions in
the margins? E.g. I'd like to get some object "joint" where one would
get :

> joint
0  1  2  3  4  f1
0  5  5  5  5  4  24
1  4  2  6  7  3  22
2  5  3  5  3  6  22
3  3  1  3  1  2  10
4  6  4  3  3  6  22
f2 23 15 22 19 21 100

So that one could then say "joint/nrow(D)" and get nice probabilities
out of it. It would great to be able to say "xtable(joint/nrow(D))" :-)

I'm sure R has a lovely way to do this, but I'm not able to figure it out.

--
Ajay Shah                                                   Consultant
ajayshah at mayin.org                      Department of Economic Affairs
http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi

```