Contingency tables in R

0 votes
What are different ways to create a contingency table? (Examples with explanation)
Aug 21, 2019 in Data Analytics by ajish
789 views

1 answer to this question.

0 votes

You can build 2 types of contingency tables.

One way table - 

table() returns simple frequency counts

Ex: 

> table(mpg$cyl)

 4  5  6  8 
81  4 79 70 

prop.table() returns the frequemcy in terms on proportion.

Ex: 

> prop.table(table(mpg$cyl))

         4          5          6          8 
0.34615385 0.01709402 0.33760684 0.29914530

Two way tables - 

table(var1,var2) returns count in row and column values.

Ex: 

table(mpg$cyl,mpg$year)
   
    1999 2008
  4   45   36
  5    0    4
  6   45   34
  8   27   43

xtabs() - returns a contingency table like table().

Here var1+var2 are the variables used to perform the table are calculated wrt full data frame.

Ex: 
xtabs(~cyl+year,mpg)
   year
cyl 1999 2008
  4   45   36
  5    0    4
  6   45   34
  8   27   43

You can see both frequency count and proportion using margin.table(table object)

answered Aug 22, 2019 by anonymous
• 33,030 points

Related Questions In Data Analytics

0 votes
1 answer

How to join two tables (tibbles) by *list* columns in R

You can use the hash from digest ...READ MORE

answered Apr 6, 2018 in Data Analytics by kappa3010
• 2,090 points
1,734 views
0 votes
1 answer

How to convert tables to a data frame in R ?

> trial.table.df <- as.data.frame(trial.table) //assuming that trial.table ...READ MORE

answered Apr 20, 2018 in Data Analytics by zombie
• 3,790 points
7,625 views
+2 votes
1 answer

Need a hadoop engine in backend to run r server

Dear Koushik, Hope you are doing great. The hadoop ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
885 views
0 votes
2 answers

Installing MXNet for R in Windows System

You can install it for python in ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 52,350 points
2,487 views
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,265 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,962 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika 1,200 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,393 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,838 views
+4 votes
3 answers

How to sum a variable by group in R?

You can also try this way, x_new = ...READ MORE

answered Aug 1, 2019 in Data Analytics by Cherukuri
• 33,030 points
78,205 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP