Contingency tables in R

0 votes
What are different ways to create a contingency table? (Examples with explanation)
Aug 21 in Data Analytics by ajish
100 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 21 by anonymous
• 31,840 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 5, 2018 in Data Analytics by kappa3010
• 2,020 points
72 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,690 points
105 views
+1 vote
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 17, 2017 in Data Analytics by Sudhir
• 1,610 points
69 views
0 votes
2 answers

Installing MXNet for R in Windows System

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

answered Dec 3, 2018 in Data Analytics by Kalgi
• 42,430 points
412 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
116 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
160 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
87 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
104 views
0 votes
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21 in Data Analytics by anonymous
• 31,840 points
149 views
0 votes
2 answers

How to sum a variable by group in R?

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

answered Jul 31 in Data Analytics by Cherukuri
• 31,840 points
10,483 views