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:

270 Comments:

«Oldest ‹Older 201 – 270 of 270
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  
Blogger how to wtite cover letter for university said...

Thank you for the long form insights. we are here to appreciate and nurture long form content rather than short form content.
Data science courses in Ghana

September 25, 2024 at 10:14 AM  
Blogger bangaloredigitalmarketing said...

Very interesting blog. Thanks a lot for sharing such a good source
digital marketing courses in bangalore
Video Editing course in bangalore

September 26, 2024 at 2:14 AM  
Blogger Rachana said...

Great job on breaking down the process of converting R contingency tables to data frames! Your clear explanations and practical tips make this topic so much easier to grasp. Keep up the amazing work; you’re empowering data enthusiasts to unlock the full potential of R!
Data Science Courses in Singapore

September 28, 2024 at 2:14 PM  
Blogger Sakshi Shah said...

This blog provides a clear and informative explanation of contingency tables in R, demonstrating how to create and manipulate them effectively. The detailed example of plant species traits, along with the accompanying code, makes it easy to understand how to work with categorical variables and their frequencies. I particularly appreciate the introduction of the as.data.frame.matrix function, which is not widely known but incredibly useful for maintaining the original structure of a table. Overall, this post is a valuable resource for anyone looking to enhance their data analysis skills in R. Great job!
data analytics courses in dubai

September 28, 2024 at 4:55 PM  
Blogger Bhumi Goswami said...

Thanks for this insightful post! Converting R contingency tables to data frames is a useful skill, and your step-by-step guide makes it easy to follow.

Data science courses in Bhutan

October 2, 2024 at 1:00 AM  
Anonymous Anonymous said...

Hello Blogger,
I'm glad you're creating documentation for the Converting R contingency tables to data frames. It's a valuable resource for anyone who needs to work with this reporting. I'm looking forward to reading your next blog post. Thank you for this one.
Online Data Science Course

October 2, 2024 at 9:46 AM  
Blogger praju said...

The topic of converting R contingency tables to data is very refreshing with relatable view. Your way to explain it is very innovtaive. Great content.
Online Data Science Course

October 3, 2024 at 3:32 AM  
Blogger Data Analytics Courses In Ontario said...

"Thanks for sharing this information about the Data Science Course in Dadar!
The course structure looks solid and comprehensive.
I appreciate the focus on both analytics and machine learning skills.
Having access to local training makes it much easier to pursue my goals.
I can’t wait to learn more about the enrollment process!

October 19, 2024 at 3:07 PM  
Blogger Sadhvi said...

Great post! Converting contingency tables to data frames in R can be tricky, especially when you want to preserve the original table structure. Data science courses in Mysore

October 26, 2024 at 8:51 AM  
Blogger Data science Courses in Norwich said...

Thank you for the wonderful blog.
Data science Courses in Germany

November 7, 2024 at 3:54 PM  
Blogger Sakshi Gupta said...

"Converting contingency tables to data frames in R is a common operation for analysis. You can use the `as.data.frame()` function to convert a contingency table into a data frame, making it easier to manipulate and visualize the data."

Data Science Course in Chennai

November 16, 2024 at 3:57 AM  
Blogger Data Analytics Courses In Ontario said...

"Great post! It's wonderful to see the rise of data science education in Iraq. With the increasing demand for data professionals, it’s great to have localized options available. For anyone interested in taking the plunge, Data science courses in Iraq are an excellent place to start!"

November 19, 2024 at 3:41 AM  
Blogger Sabha Singh said...

Thank you for sharing this helpful guide on converting R contingency tables to data frames! The post simplifies the process, making it easier for those working with data in R to manipulate and analyze their contingency tables. The example-driven approach ensures a clear understanding, which is great for both new and experienced users.

Data science course in Lucknow

November 20, 2024 at 2:26 AM  
Blogger RICHA said...

This blog provides a clear method for converting R contingency tables to data frames, making it easier to manipulate and analyze data. A great resource for R users working with data structures!
Data science course in Gurgaon

November 20, 2024 at 6:11 AM  
Blogger Sunaina kaur said...

Nice, detailed article.
Data science courses in chennai

November 22, 2024 at 9:17 AM  
Blogger P. Zaheer Khan said...

Great blog! The explanation on converting R contingency tables to data frames is clear and practical. This is super helpful for simplifying data manipulation in R. appreciate for this examples.
Data science course in Bangalore

November 22, 2024 at 11:30 PM  
Blogger iim skills Diksha said...

Fantastic Blog! Thank you for the information.
Digital marketing courses in mumbai

November 28, 2024 at 9:10 PM  
Blogger NB said...

Thanks for the valuable information! It was really useful.
GST Course

December 14, 2024 at 10:43 AM  
«Oldest ‹Older 201 – 270 of 270

Post a Comment

Subscribe to Post Comments [Atom]

<< Home