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 :> joint0 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
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
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
On Sun, 3 Oct 2004, Ajay Shah wrote:> Suppose I make two discrete variables -- >> D <- data.frame(f1=sample(1:5,100,replace=T), f2=sample(1:5,100,replace=T)) ><sniP>> 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 :?addmargins. -thomas
Thanks to everyone who helped me solve this question. My cleanest
solution is:
joint.and.marginals <- function(x,y) {
t <- addmargins(table(x, y))
rownames(t)[nrow(t)] <- deparse(substitute(y))
colnames(t)[ncol(t)] <- deparse(substitute(x))
return(t)
}
There are many other valid solutions, but this one struck me as being
the simplest.
As a demo of it's use:
> D <- data.frame(f1=sample(1:5,10000,replace=T),
f2=sample(1:5,10000,replace=T)
> system.time(print(joint.and.marginals(D$f1, D$f2)))
y
x 1 2 3 4 5 D$f1
1 420 427 385 376 423 2031
2 425 432 429 375 347 2008
3 405 419 434 401 352 2011
4 374 374 370 417 403 1938
5 403 381 409 388 431 2012
D$f2 2027 2033 2027 1957 1956 10000
[1] 0.05 0.00 0.07 0.00 0.00
Hmm, how would one get rid of the 'x' and 'y' that are occuring
in the
table? :-)
-ans.