# [R] Making a 'joint distribution'?

John Fox jfox at mcmaster.ca
Tue Oct 5 15:30:26 CEST 2004

```Dear Ajay,

There may be a function that does this already, but if not, it not hard to
do, at least for two-way tables:

Table <- function(x, y) {
tab <- table(x, y)
tab <- cbind(tab, rowSums(tab))
tab <- rbind(tab, colSums(tab))
rownames(tab)[nrow(tab)] <- deparse(substitute(y))
colnames(tab)[ncol(tab)] <- deparse(substitute(x))
tab
}

I hope this helps,
John

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ajay Shah
> Sent: Sunday, October 03, 2004 4:10 AM
> To: r-help
> Subject: [R] Making a 'joint distribution'?
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help