Faculty of : FCE Program: [Link] Class/Section: Sem V, Sec.
A,B,C(AIDS) Date:
Name of Faculty: Seema Kaloria Name of Course: R Programming Code: BADCCE5104
Contingency Table:
A contingency table (also known as a cross-tabulation or crosstab) is a table used to display the frequency
distribution of two or more categorical variables. In R, you can create a contingency table from vectors using
the table() function.
1. Creating a Contingency Table from Vectors
Suppose you have two categorical vectors (e.g., gender and favorite color). You can create a contingency table
to see the frequency distribution of these two variables.
Example:
# Vectors representing categorical variables
gender <- c("Male", "Female", "Female", "Male", "Male", "Female", "Female", "Male")
favorite_color <- c("Blue", "Red", "Red", "Green", "Blue", "Blue", "Red", "Green")
# Creating the contingency table
contingency_table <- table(gender, favorite_color)
print(contingency_table)
Output:
favorite_color
gender Blue Green Red
Female 1 0 3
Male 2 2 0
In this example:
The rows represent gender.
The columns represent favorite_color.
The values in the table represent the count of occurrences for each combination of gender and
favorite_color.
2. Adding Margins to the Contingency Table
You can add row and column totals (margins) to the table using the addmargins() function.
# Adding row and column totals
table_with_margins <- addmargins(contingency_table)
print(table_with_margins)
Session 2024-25
Output:
favorite_color
gender Blue Green Red Sum
Female 1 0 3 4
Male 2 2 0 4
Sum 3 2 3 8
3. Proportions in Contingency Table
a) Cell Proportions
You can calculate the proportions of the table using [Link]().
# Proportions of the total
prop_table <- [Link](contingency_table)
print(prop_table)
Output:
favorite_color
gender Blue Green Red
Female 0.125 0.000 0.375
Male 0.250 0.250 0.000
This shows the proportion of each cell in the entire table.
b) Row-wise Proportions
To get the proportions within each row (e.g., within each gender group):
# Proportions by row
prop_table_row <- [Link](contingency_table, margin = 1)
print(prop_table_row)
Output:
favorite_color
gender Blue Green Red
Female 0.25 0.00 0.75
Male 0.50 0.50 0.00
This tells you the proportion of each favorite color within the Female and Male categories.
c) Column-wise Proportions
Similarly, to get proportions within each column (e.g., within each favorite color group):
# Proportions by column
prop_table_col <- [Link](contingency_table, margin = 2)
print(prop_table_col)
Output:
favorite_color
gender Blue Green Red
Female 0.333 0.000 1.000
Male 0.667 1.000 0.000
Session 2024-25
This gives you the proportion of each gender within each color category.
4. Working with Data Frames
You can also create contingency tables from data frames. If the variables are columns in a data frame, use the
table() function directly on those columns.
Example:
# Create a data frame
data <- [Link](
gender = c("Male", "Female", "Female", "Male", "Male", "Female", "Female", "Male"),
favorite_color = c("Blue", "Red", "Red", "Green", "Blue", "Blue", "Red", "Green")
)
# Creating a contingency table from data frame
contingency_table_df <- table(data$gender, data$favorite_color)
print(contingency_table_df)
5. Visualization of Contingency Tables
You can visualize contingency tables using bar plots or mosaic plots in R.
a) Bar Plot
barplot(contingency_table, beside = TRUE, col = c("lightblue", "lightgreen"))
legend("topright", legend = rownames(contingency_table), fill = c("lightblue",
"lightgreen"))
b) Mosaic Plot
mosaicplot(contingency_table, main = "Mosaic Plot of Gender and Favorite Color", color =
TRUE)
Session 2024-25
Session 2024-25
Session 2024-25
Session 2024-25
Session 2024-25