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:

254 Comments:

«Oldest ‹Older 201 – 254 of 254
Blogger Đồ gia dụng said...

Địa chỉ lắp đặt cửa lưới chống muỗi giá rẻ nhất ở đâu

March 19, 2021 at 6:47 AM  
Blogger IamLinkfeeder said...

It was really fun reading ypur article. Thankyou very much. # BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page
Our Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
Boost DA upto 15+ at cheapest
Boost DA upto 25+ at cheapest
Boost DA upto 35+ at cheapest
Boost DA upto 45+ at cheapest

March 25, 2021 at 6:06 AM  
Blogger naturenuskha said...

Thank you for excellent article.You made an article that is interesting. Nurture through nature

May 20, 2021 at 8:23 AM  
Blogger Corina McDonald said...



Right now I am watching NFL online with N F L H D G A M E , This NFL is available with no extra cost.

Free TRIAL Here => NFLHDGAME.COM

NFL HD GAME . COM
N F L H D G A M E . C O M
N F L H D G A M E
NFLHDGAME
NFL HD GAME
nflhdgame. com
nfl hd game . com
n f l hd g a m e . com
nflhdgame
nfl hd game
n f l h d g a m e



May 22, 2021 at 3:22 PM  
Blogger salome said...

very interesting to read.thanks for sharing.Angular training in Chennai

May 29, 2021 at 12:08 PM  
Blogger sophiawebnet01 said...

"mcafee is an antivirus software providers that secure your computer for virus , worms ,trojens and other mailcious program .it provides full range of
security product like antivirus , firewall etc .you have to do mcafee antivirus download "

July 13, 2021 at 3:04 AM  
Blogger Đồ gia dụng said...

Nên nâng cấp máy tính hay là mua mới
Thủ thuật vệ sinh máy tính để bàn
Máy tính để bàn chơi game nên chọn cấu hình như thế nào

September 17, 2021 at 5:16 AM  
Blogger Pranisha said...

Quick up for the best offer of Azure Training in Chennai from Infycle Technologies, Excellent software training in Chennai. A massive place to learn other technical courses like Power BI, Cyber Security, Graphic Design and Animation, Block Security, Java, Oracle, Python, Big data, Azure, Python, Manual and Automation Testing, DevOps, Medical Coding etc., with outstanding training with experienced trainers with a fresh environment with 100+ Live Practical Sessions and Real-Time scenario after the finalisation of the course the trainee will able to get through the interview in top MNC’s with an amazing package for more enquiry approach us on 7504633633, 7502633633

February 4, 2022 at 9:16 AM  
Blogger Unknown said...

I think that you should definitely do it again sometime and hopefully I will be able to make it to that one
leather jacket
wedding photography

April 20, 2022 at 5:35 AM  
Blogger Emerging Technologies said...

I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details. data science course in pune

June 3, 2022 at 9:31 AM  
Anonymous Anonymous said...

You have amazing writing skills and you display them in every article. Keep it going!
Best Cyber Security Training Programs

August 12, 2022 at 5:16 PM  
Blogger DLK Technologies said...

Thank you so much for sharing this information. Do visit be project centers in Chennai

August 20, 2022 at 8:23 AM  
Blogger Sruthi Karan said...


Very interesting post and I want more updates from your blog. Thanks for your great efforts.
Virginia Online Divorce
How to get a Divorce in VA

September 6, 2022 at 10:20 AM  
Blogger shazam said...

Very interesting posts
Abogado Disputas Contratos Comerciales
Disputas Contratos Litigio Mediación

November 23, 2022 at 6:04 AM  
Blogger desmond miles said...

Thankyou for your information and it was realy helplul anyways.
The following link is given below :
Dental Implants in Chennai

February 1, 2023 at 2:44 AM  
Blogger homieserver said...

شركة كشف تسربات بالقصيم

August 27, 2023 at 7:23 PM  
Blogger homieserver said...

https://su-
qema.com/%D8%B4%D8%B1%D9%83%D8%A9-%D9%83%D8%B4%D9%81-%D8%AA%D8%B3%D8%B1%D8%A8%D8%A7%D8%AA-%D8%A7%D9%84%D9%85%D9%8A%D8%A7%D9%87-%D8%A8%D8%A8%D8%B1%D9%8A%D8%AF%D8%A9/

August 27, 2023 at 7:28 PM  
Blogger night owl said...

"Thank you for this handy tip! Converting contingency tables to data frames can be tricky, and your solution is a time-saver. Much appreciated!"
Data Analytics Courses In Bangalore

September 1, 2023 at 1:03 PM  
Anonymous Anonymous said...

A practical article that likely explains how to convert contingency tables into data frames in R, providing useful insights for data manipulation and analysis.

Data Analytics Courses In Kochi



September 9, 2023 at 11:20 AM  
Blogger Aruna Sen said...

Hi,
This post provides a valuable solution to a common issue encountered when working with contingency tables in R. The explanation is clear and concise, making it easy for readers to understand how to maintain the original table structure when converting it into a data frame. A practical and helpful tip.
Data Analytics Courses In Dubai

September 19, 2023 at 7:55 AM  
Blogger Data Analytics Courses in Agra said...

Good day, Blogger, Excellent idea. Informative post. Thanks.
Data Analytics Courses in Agra

September 30, 2023 at 3:22 AM  
Blogger Pratyaksha said...

It simplifies a potentially complex process and provides a clear step-by-step guide. Thanks for making this task.
Data Analytics Courses In Chennai

October 1, 2023 at 10:35 AM  
Blogger datavadodara said...

good blog
Data Analytics Courses In Vadodara

October 5, 2023 at 3:29 PM  
Blogger Programming Edu said...

Informative, engaging, and always up-to-date, this blog is a valuable resource for both beginners and tech enthusiasts. Check out IT Certification Course by SkillUp Online.

October 12, 2023 at 6:53 AM  
Blogger Surabhi said...

Converting R contingency tables to data frames is a crucial step in data manipulation and analysis, facilitating further statistical exploration and modeling.

In the field of data analytics, Glasgow's Data Analytics courses offer in-depth training on R and its versatile capabilities, providing professionals with the skills to work with complex data structures effectively. Please also read Data Analytics courses in Glasgow.

October 13, 2023 at 5:21 AM  
Blogger Spy Shop Online said...

Buy spy gadgets online and discover a world of secret recording, camera detectors, and more. As a trusted wholesaler and store, we provide top-quality products for your ultimate peace of mind. Revolutionize your security with Spy Shop Online today!

October 18, 2023 at 4:08 AM  
Blogger Sathya said...

Thanks for the insightful article. Consider Digital Academy 360 to kickstart your digital marketing courses journey with expert training and hands-on experience. Best wishes

October 26, 2023 at 2:55 AM  
Blogger IIM skills said...

This article is a goldmine of information. Thanks for the insights!

October 27, 2023 at 9:04 AM  
Blogger IIM skills said...

This article is a goldmine of information. Thanks for the insights.

October 28, 2023 at 4:55 AM  
Blogger Digital marketing courses in Reading said...

Transforming those tables made my data analysis a breeze. Thanks for simplifying a complex process!
Digital marketing courses in Reading

November 1, 2023 at 11:36 PM  
Blogger Rupesh Kumar said...

Nice post. I was checking constantly this blog and I’m impressed. Are you facing any of these challenges for online English tuition? Like Grammar and Syntax, Spoken English and Pronunciation, Job Interview skills.
For more info visit English grammar classes

November 2, 2023 at 3:27 AM  
Blogger Digital marketing said...

I learned so much from this post. It's like a mini-education in the subject matter.

November 2, 2023 at 11:02 AM  
Blogger Diluk said...

Hello blogger, it is great read entirely defined ,
Digital marketing courses in Blackpool

November 5, 2023 at 1:44 PM  
Blogger DA in limerick said...

Thanks for sharing informative and detailed tutorial on Converting R contingency tables to data frames.
data analyst courses in limerick

November 9, 2023 at 5:49 AM  
Blogger sayaniimskillseo said...

very informative blog. properly explained. keep up the good work.
financial modelling course in melbourne

November 11, 2023 at 5:26 AM  
Blogger digital marketing course in norwich said...

I appreciate the attention to detail in explaining different conversion methods and the considerations for choosing the most suitable approach based on specific requirements. This thoughtful approach not only educates users on the "how" but also empowers them to make informed decisions in their data analysis workflows. Digital Marketing Courses In Norwich

November 27, 2023 at 1:28 PM  
Blogger Deepa Rai said...

Converting R contingency tables to data frames allows for easier manipulation and analysis of categorical variable relationships. By transforming counts of factor levels into a structured data frame format, one gains flexibility in exploring associations between plant species, seed sizes, and growth forms for more comprehensive insights and visualizations. This conversion enhances the accessibility and versatility of categorical data analysis within R.
Data Analytics courses in new york

December 5, 2023 at 1:08 AM  
Blogger Mishra A said...

Your writing is incredibly captivating; I found it hard to put down! The eloquence with which you convey ideas is truly engaging and enthralling.
Best Software Training Institute In Electronic City Bangalore

December 14, 2023 at 4:04 AM  
Blogger Zika Rakita said...

Hi, thanks for sharing valuable info.

investment banking courses with placement
intelligence analysis services
bodyguards for hire
SEO copywriter for hire

December 15, 2023 at 2:41 PM  
Blogger Sayaniimskillsseo said...

very beautifully curated blog post, really well written
Investment banking courses in Jabalpur

December 16, 2023 at 12:20 AM  
Blogger Bhavya said...

Converting contingency tables to data frames in R facilitates further analysis and manipulation. The process involves transforming the table structure into a more versatile and accessible format. By converting to a data frame, you gain the ability to use a wide range of R functions and packages.
investment banking free course

December 17, 2023 at 12:16 PM  
Blogger Investment Banking Courses Syllabus said...

Thank you for elucidating the process of converting R contingency tables to data frames. Your clear explanation and use of practical examples make it easier for readers to understand this nuanced aspect. Great post!

Investment banking courses syllabus

December 18, 2023 at 4:57 AM  
Blogger Investment Banking Courses in Singapore said...

This is most informative POST.

Investment banking courses in Singapore

December 21, 2023 at 2:09 PM  
Blogger aaravgupta said...

Hey there! Converting R contingency tables to data frames can be really useful for further analysis and manipulation. It's great to have the flexibility of working with data frames in R. Thanks for sharing this tip!
Data analytics courses in Rohini

December 22, 2023 at 11:46 AM  
Blogger nandhu said...

Nice blog.. thank you for sharing..
best project center in Chennai| embedded project center| mechanical project center| android project center| matlab project center| Iot project center

December 28, 2023 at 9:57 AM  
Blogger Investment Banking Courses in Singapore said...

Very informative.

Investment banking courses in Singapore

December 29, 2023 at 1:20 PM  
Blogger Ajay said...

Students who are pursuing BSc from various universities can check their bsc time table online and prepare for their exams accordingly.

January 9, 2024 at 1:29 AM  
Blogger Signature Luxury Transportation Group said...

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

Signature Luxury Transportation Group

Signature Luxury Transportation Group

January 29, 2024 at 6:04 AM  
Blogger Signature Luxury Transportation Group said...

Your blog is splendid, I follow and read continuously the blogs that you share, they have some really important information. M glad to be in touch plz keep up the good work.
Signature Luxury Transportation Group

Signature Luxury Transportation Group

January 29, 2024 at 6:06 AM  
Blogger nandhu said...

Nice blog... Thanks for sharing
Best Project Center in Chennai

February 2, 2024 at 1:51 AM  
Blogger superkhdma said...

This comment has been removed by the author.

February 13, 2024 at 10:18 AM  
Blogger jazz said...

that table of data had used kitchen equipment for sale they were really good supplier i need them

April 1, 2024 at 12:43 PM  
Blogger jazz said...

cow milk near me

April 3, 2024 at 10:26 AM  
Blogger Kalp said...

great post! if you like Food Delivery App Development Company then visit our site

April 19, 2024 at 11:15 AM  
«Oldest ‹Older 201 – 254 of 254

Post a Comment

Subscribe to Post Comments [Atom]

<< Home