2010-08-11

Converting R contingency tables to data frames

A contingency table presents the joint density of one or more categorical variables. Each entry in a contingency table is a count of the number of times a particular set of factors levels occurs in the dataset. For example, consider a list of plant species where each species is assigned a relative seed size (small, medium, or large) and a growth form (tree, shrub, or herb).

seed.sizes <- c("small", "medium", "large")
growth.forms <- c("tree", "shrub", "herb")
species.traits <- data.frame(
  seed.size = seed.sizes[c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3)],
  growth.form = growth.forms[c(3, 3, 2, 2, 1, 2, 2, 3, 1, 1, 1, 1)]
)
seed.sizegrowth.form
smallherb
smallherb
smallshrub
smallshrub
smalltree
mediumshrub
mediumshrub
mediumherb
mediumtree
largetree
largetree
largetree

A contingency table will tell us how many times each combination of seeds.sizes and growth.forms occur.

tbl <- table(species.traits)
herbshrubtree
003
121
221

The output contingency table are of class table. The behaviour of these objects is not quite like a data frame. In fact, trying to convert them to a data frame gives a non-intuitive result.

as.data.frame(tbl)
seed.sizegrowth.formFreq
largeherb0
mediumherb1
smallherb2
largeshrub0
mediumshrub2
smallshrub2
largetree3
mediumtree1
smalltree1

Coercion of the table into a data frame puts each factor of the contingency table into its own column along with the frequency, rather than keeping the same structure as original table object. If we wanted to turn the table into a data frame keeping the original structure we use as.data.frame.matrix. This function is not well-documented in R, and this is probably the only situation in which it would be used. But, it works.

as.data.frame.matrix(tbl)
herbshrubtree
003
121
221

Labels:

10 Comments:

Blogger Jesmin said...

thanx,
its awsome and make work loud lighter,,its easy to calculate now with your contingency table

September 22, 2010 at 4:33 AM  
Blogger Scellus said...

Great, thanks! (It was hard to find this.)
Try help(as.data.frame.matrix) - well hidden indeed.

May 26, 2011 at 6:50 AM  
Blogger Yogalakshmi said...

Valuable function I was looking for something like this. Thanks.

January 10, 2013 at 3:33 AM  
Blogger lee woo said...

Strength and growth come only through continuous effort and struggle. See the link below for more info.


#growth
www.ufgop.org

July 3, 2015 at 3:40 AM  
Blogger Silvia Jacinto said...

Good day! I found your site very interesting and informative . Thanks for taking time sharing it with us. I really enjoyed reading your post.

triciajoy.com

www.triciajoy.com

October 7, 2015 at 10:03 PM  
Blogger raphael said...

Awesome it is a beautiful thing you had posted. Thank you for posting and please update like this information with this site.

digital marketing training Chennai

February 25, 2016 at 5:24 AM  
Blogger kanmani karthick said...

In the provided information i agree our all information and really very nice all articles ,thanks for sharing in this post.
I like in this post.
Oracle SQL Training in Chennai

March 2, 2016 at 2:38 AM  
Blogger Aamala Ahona said...



All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.


Peridot Systems Adyar Contact Number

March 22, 2016 at 8:16 AM  
Blogger Ridhima said...


Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up.

SEO training in Adyar

April 2, 2016 at 8:33 AM  
Blogger Priscilla said...

This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the
time to discuss this, I feel happy about it and I love learning more about this topic..

Peridot Systems Adyar Complaints

April 13, 2016 at 9:06 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home