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:

23 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  
Blogger arulnath d said...

you have a greatest updated knoweledge in your field... tnk to share withus...
Seo training in Chennai

May 11, 2016 at 3:13 AM  
Blogger Priya Sethuraman said...

Really very nice blog information for this one and more technical skills are improve,i like that kind of post.


Digital Marketing Training in Chennai

May 11, 2016 at 10:09 AM  
Blogger Suseela Susiee said...

Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


SEO Company in Chennai

June 21, 2016 at 7:58 AM  
Blogger Keerthi said...

Great articles, first of all Thanks for writing such lovely Post! Earlier I thought that posts are the only most important thing on any blog. But here at Shoutmeloud I found how important other elements are for your blog.Keep update more posts..

Online Training in Chennai

July 19, 2016 at 2:58 AM  
Blogger prethikarajesh said...

Thank you for having taken your time to provide us with your valuable information relating to your stay with us.we are sincerely concerned.., Most importantly, you Keepit the major...
seo company in chennai

July 25, 2016 at 5:15 AM  
Blogger Maani kamili said...

This blog explains the details of most popular technological details. This helps to learn about what are all the different method is there. And the working methods all of that are explained here. Informative blog.
Digital Marketing For Small Business in Chennai

July 26, 2016 at 8:49 AM  
Blogger sindhu said...

I do believe all of the concepts you’ve introduced in your post. They’re very convincing and will definitely work. Nonetheless, the posts are too short for novices. May you please extend them a bit from subsequent time? Thank you for the post.

SMO Services Chennai

July 27, 2016 at 8:35 AM  
Blogger kanchana said...

Your blog is more informative..Its very interesting to know about new things..Keep on sharing.
Hadoop training in chennai

August 3, 2016 at 9:26 AM  
Blogger Rekhila Pk said...

Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks.
Hadoop training in chennai

August 5, 2016 at 8:53 AM  
Blogger Aashi siva said...

Someone essentially lend a hand to make severely posts I would state. That is the very first time I frequented your website page and thus far? I surprised with the analysis you made to create this particular submit incredible. Fantastic job!

Corporate Training in Chennai

August 10, 2016 at 8:56 AM  
Blogger Girija Manikandan said...

Wowwww... really great information. Having interesting topic. The pictorial representation helps me to easy understanding of this. Have a great idea. Thank you.
SEO Web Design Company in Chennai

August 11, 2016 at 7:20 AM  
Blogger Harish Raju said...

Thanks for this blog. All screenshots can explain the full blogs easily. This is very easy to understand. Using this screen shots know the details becomes easy.
Digital Marketing Company in Chennai

August 24, 2016 at 2:51 AM  
Blogger Geethapriya said...

very useful information provided in this blog. concepts were explained in a detailed manner. Keep giving these types of information.
SEO Training in Chennai

August 29, 2016 at 6:23 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home