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.

Summary Functions for Building Contingency Tables in R
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

1 Build a Simple Contingency Table (Two-way Frequency Table) in R

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
table(meals)
      Meal
Names  Beans Bread Noodles Rice
  Adam     1     0       0    2
  Dave     0     1       1    1
  Sam      0     1       2    0

2 Build a Contingency Table (Two-way Frequency Table) from a DataFrame in R

Using the HairEyeColor data from the "datasets" package.

Sample rows from HairEyeColor as a dataframe:

HC = data.frame(HairEyeColor)
HC
    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:

xtabs(Freq ~ Hair + Eye, HC)
       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:

xtabs(Freq ~ Hair + Eye,
      exclude = c("Hazel", "Red") , HC)
       Eye
Hair    Brown Blue Green
  Black    68   20     5
  Brown   119   84    29
  Blond     7   94    16

2x2 table subsetting the data:

xtabs(Freq ~ Hair + Eye,
      subset = Sex == "Male" & Eye == c("Brown", "Hazel"), HC)
       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

3 Contingency Table (Two-way Frequency Table) with Sums or Proportions in R

See sample rows from the HC dataframe used here above.

Row sums and column sums of 2x2 contingency table:

fsum = xtabs(Freq ~ Hair + Eye, HC)
fsum
       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
# Sums on margins
addmargins(fsum)
       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
# Overall Sum
marginSums(fsum)
[1] 592
# Sum of rows
marginSums(fsum, "Hair") # or marginSums(fsum, 1)
Hair
Black Brown   Red Blond 
  108   286    71   127 
# Sum of columns
marginSums(fsum, "Eye") # or marginSums(fsum, 2)
Eye
Brown  Blue Hazel Green 
  220   215    93    64 

Row proportions and column proportions of 2x2 contingency table:

fprop = xtabs(Freq ~ Hair + Sex, HC)
fprop
       Sex
Hair    Male Female
  Black   56     52
  Brown  143    143
  Red     34     37
  Blond   46     81
# Overall proportions
prop.table(fprop)
       Sex
Hair          Male     Female
  Black 0.09459459 0.08783784
  Brown 0.24155405 0.24155405
  Red   0.05743243 0.06250000
  Blond 0.07770270 0.13682432
# Proportions by rows
prop.table(fprop, "Hair") # or prop.table(fprop, 1)
       Sex
Hair         Male    Female
  Black 0.5185185 0.4814815
  Brown 0.5000000 0.5000000
  Red   0.4788732 0.5211268
  Blond 0.3622047 0.6377953
# Proportions by columns
prop.table(fprop, "Sex") # or prop.table(fprop, 2)
       Sex
Hair         Male    Female
  Black 0.2007168 0.1661342
  Brown 0.5125448 0.4568690
  Red   0.1218638 0.1182109
  Blond 0.1648746 0.2587859

4 Three-way Table in R

See sample rows from the HC dataframe used here above.

For a three-way table, simply specify the three variables:

xtabs(Freq ~ Hair + Eye + Sex, HC)
, , 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

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes