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)]

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

tbl <- table(species.traits)

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.


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.




Blogger Jesmin said...

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.


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.



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  
Blogger Jeffy said...

Thank you for sharing such a nice and interesting blog with us. Hope it might be much useful for us. keep on updating...!!
seo company in india
digital marketing company in india

December 22, 2016 at 12:42 AM  
Blogger Shalini said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
Digital Marketing Company in India

January 20, 2017 at 11:53 PM  
Blogger karthik anu said...

This comment has been removed by the author.

January 24, 2017 at 2:35 AM  
Blogger karthik anu said...

You have explained the conversions of R contingency into tables, your explanation was good and it is in an understandable way.iOS Training in Chennai
Android Training in Chennai
php Training in Chennai

January 24, 2017 at 2:36 AM  
Blogger bavana princy said...

This blog having the details of Processes running. The way of running is explained clearly. The content quality is really great. The full document is entirely amazing. Thank you very much for this blog.
SEO Company in India

February 19, 2017 at 11:09 PM  
Blogger نور الهدى said...

شركات نقل اثاث بالمدينة المنورة تساعدك على نقل اثاثك بامان فلا داعى للقلق مع شركات نقل اثاث بجدة

عزيزى العميل انت من محبى التنقل باستمرار بالتالى انت بحاجة ماسة وضروية الى الاستعانة بالمختصين في نقل العفش خاصة شركات نقل اثاث بالرياض لان الاستعانة باى من عمالة الشوارع الغير مدربة والتي لا تمتلك خبرة كافية في نقل العفش او الحفاظ علية وليس هذا فقط فقد يؤدى الاستعانة بعمالة الشوارع الى حدوث حالة فقدان وتكسير للاثاث بالتالى التاثير الضار عليك عزيزى العميل

لا تقلق مطلقاً الان بشأن نقل اي منقولات خاصة بك طالما استعنت بشركة الاول لـ نقل الأثاث في الرياض وخارج الرياض فنحن ليس الوحيدون ولكننا متميزون عن اى مؤسسة أخرى داخل وخارج الدمام وشهرتنا كافضل شركة نقل عفش بينبع
نقل عفش بجدة

February 22, 2017 at 3:30 PM  
Blogger Guillermo Groce said...

You can Watch UFC 210 live streaming here:

UFC 210 live streaming
UFC 210 live stream
Cormier vs Johnson 2 Live Stream

UFC 210 live streaming
UFC 210 live stream

Cormier vs Johnson 2 Live Streaming
Cormier vs Johnson 2 Live Stream
UFC 210 live stream
UFC 210 live streaming

You can Watch NFL live streaming here:

Watch NFL Games
NFL Live streaming
NFL Live stream
NFL Live streaming

March 19, 2017 at 5:04 PM  
Blogger Evangeline Mitul said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.

Laser Root Canal Treatment In Chennai

Best Dental Clinic In Velachery

March 22, 2017 at 8:13 AM  
Blogger tessa tessa 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 a Shout me loud found how important other elements are for your blog.Keep update more posts..

SEO Company in India

March 30, 2017 at 2:07 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home