Here, we show how to create contingency tables (or two-way frequency
table) in R using the table()
and xtabs()
functions.
table()
is from the "base" package, while
xtabs()
is from the "stats" package.
See also frequency tables.
Function | Usage |
table() |
Build a contingency table |
xtabs() |
Build a contingency table from a dataframe |
addmargins() |
Add rows and columns sums to a contingency table |
marginSums() |
Get rows or columns sums of a contingency |
prop.table() |
Get cell proportions by rows, columns or overall |
Enter the data by hand:
Names = c("Adam", "Adam", "Adam",
"Dave", "Dave", "Dave",
"Sam", "Sam", "Sam")
Meal = c("Beans", "Rice", "Rice",
"Noodles", "Rice", "Bread",
"Bread", "Noodles", "Noodles")
meals = data.frame(Names, Meal)
meals
Names Meal
1 Adam Beans
2 Adam Rice
3 Adam Rice
4 Dave Noodles
5 Dave Rice
6 Dave Bread
7 Sam Bread
8 Sam Noodles
9 Sam Noodles
Meal
Names Beans Bread Noodles Rice
Adam 1 0 0 2
Dave 0 1 1 1
Sam 0 1 2 0
Using the HairEyeColor data from the "datasets" package.
Sample rows from HairEyeColor as a dataframe:
Hair Eye Sex Freq
1 Black Brown Male 32
3 Red Brown Male 10
6 Brown Blue Male 50
15 Red Green Male 7
17 Black Brown Female 36
24 Blond Blue Female 64
26 Brown Hazel Female 29
27 Red Hazel Female 7
31 Red Green Female 7
32 Blond Green Female 8
2x2 contingency table:
Eye
Hair Brown Blue Hazel Green
Black 68 20 15 5
Brown 119 84 54 29
Red 26 17 14 14
Blond 7 94 10 16
2x2 table excluding some factors:
Eye
Hair Brown Blue Green
Black 68 20 5
Brown 119 84 29
Blond 7 94 16
2x2 table subsetting the data:
Eye
Hair Brown Blue Hazel Green
Black 32 0 0 0
Brown 0 0 25 0
Red 10 0 0 0
Blond 0 0 5 0
See sample rows from the HC dataframe used here above.
Row sums and column sums of 2x2 contingency table:
Eye
Hair Brown Blue Hazel Green
Black 68 20 15 5
Brown 119 84 54 29
Red 26 17 14 14
Blond 7 94 10 16
Eye
Hair Brown Blue Hazel Green Sum
Black 68 20 15 5 108
Brown 119 84 54 29 286
Red 26 17 14 14 71
Blond 7 94 10 16 127
Sum 220 215 93 64 592
[1] 592
Hair
Black Brown Red Blond
108 286 71 127
Eye
Brown Blue Hazel Green
220 215 93 64
Row proportions and column proportions of 2x2 contingency table:
Sex
Hair Male Female
Black 56 52
Brown 143 143
Red 34 37
Blond 46 81
Sex
Hair Male Female
Black 0.09459459 0.08783784
Brown 0.24155405 0.24155405
Red 0.05743243 0.06250000
Blond 0.07770270 0.13682432
Sex
Hair Male Female
Black 0.5185185 0.4814815
Brown 0.5000000 0.5000000
Red 0.4788732 0.5211268
Blond 0.3622047 0.6377953
Sex
Hair Male Female
Black 0.2007168 0.1661342
Brown 0.5125448 0.4568690
Red 0.1218638 0.1182109
Blond 0.1648746 0.2587859
See sample rows from the HC dataframe used here above.
For a three-way table, simply specify the three variables:
, , Sex = Male
Eye
Hair Brown Blue Hazel Green
Black 32 11 10 3
Brown 53 50 25 15
Red 10 10 7 7
Blond 3 30 5 8
, , Sex = Female
Eye
Hair Brown Blue Hazel Green
Black 36 9 5 2
Brown 66 34 29 14
Red 16 7 7 7
Blond 4 64 5 8
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes