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:

51 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  
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  
Blogger John Alert said...

Very informative article. I liked it.
corporate training in chennai corporate training in chennai corporate training companies in india corporate training companies in india corporate training companies in chennai corporate training companies in chennai Angular 2 Corporate Training Angular 2 Corporate Training

April 15, 2017 at 2:04 AM  
Blogger vignesjoseph said...

It's very best beneficial blogs.I read this subjects blog such a great blog and good sharing I'll be like this information post.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.SEO Training in Chennai | SEO Training Institute | Digital Marketing Training in Chennai

April 28, 2017 at 12:18 AM  
Blogger sri harsha said...



TS DSC 2017
Telangana DSC 2017
TS DSC Notification 2017
TS DSC Model Papers 2017
Sakshi TS DSC Model Papers 2017
Namasthe Telangana TS DSC Model Papers 2017
TS DSC District wise Vacancies list 2017

June 4, 2017 at 10:54 AM  
Blogger john stany said...

I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.

Android App Development Company

June 15, 2017 at 3:11 AM  
Blogger sandhosh said...

Really Good article.provided a helpful information.keep updating...
E-mail marketing company in india

June 22, 2017 at 7:44 AM  
Blogger sri harsha said...


Telangana DSC Recruitment 2017 Notification is announced in last week of December with District wise Vacancies lists TS DSC 2017 , Telangana DSC 2017, TS DSC Notification 2017, TS DSC Model Papers 2017, Sakshi TS DSC Model Papers 2017, Namasthe Telangana TS DSC Model Papers 2017, TS DSC District wise Vacancies list 2017 Government Primary and Secondary Schools for Secondary Grade Teacher (SGT),Language Pandit (LP),School Assistant(SA)and Physical Education Teacher (PET) Posts with the TS DSC 2017.

June 26, 2017 at 1:45 PM  
Blogger Nandhini said...

Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.... R Programming Training in Bangalore

July 5, 2017 at 1:28 AM  
Blogger john stany said...

You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
iOS App Development Company
iOS App Development Company

July 5, 2017 at 1:50 AM  
Blogger louis philip said...

I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

Fitness SMS
Fitness Text
Salon SMS
Salon Text
Investor Relation SMS
Investor Relation Text

July 12, 2017 at 10:12 AM  
Blogger Sowmiya said...

Interesting blog about converting r contingency tables to data which attracted me more.Spend a worthful time.keep updating more.
Informatica Training in Chennai

July 31, 2017 at 3:21 AM  
Blogger sai venkat said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…

R Programming Online Training|
Hadoop Online Training
Data Science Online Training|

August 2, 2017 at 3:50 AM  
Blogger model said...

naasongs
naasong
atozmp3

August 17, 2017 at 11:30 PM  
Blogger jessica said...

This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Mobile Marketing Service
Mobile Marketing Companies
Texting API
Sms API
sms marketing

August 24, 2017 at 3:16 AM  
Blogger kingrani said...

flipkart is one of the top eCommerce site in India here Flipkart Bank Cashback offersso
flipkart is one of the top eCommerce site in India here Flipkart Bank offersso

September 11, 2017 at 7:36 AM  
Blogger model said...

naasongs
mynaasongs
atozmp3

September 13, 2017 at 11:19 PM  
Blogger louis philip said...

I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
Sms marketing
Text message marketing
Fitness SMS

October 5, 2017 at 2:31 AM  
Blogger Venkata Subbaiah Boga said...


Today Employment News 2018
State wise Recruitments 2018
Govt.Jobs Notifications
Sarkari Naukri Jobs 2018

November 13, 2017 at 1:23 AM  
Blogger Venkata Subbaiah Boga said...

Kar 2nd PUC Model Papers 2018
RBSE 12th Model Papers 2018
JKBOSE 12th Model Papers 2018
MP 12th Subject Wise Blue Print
AHSEC HS/12th Model Papers 2018

November 13, 2017 at 1:23 AM  
Blogger Top beauty parlours in Bangalore said...

It's Really A Great Post
Best Elegant IT Services

November 15, 2017 at 7:35 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home