How to make a frequency table in R, you can use the table() function to create a frequency table also known as a contingency table, which is a powerful tool for summarizing categorical data. It displays the frequency or count of each unique value in various R objects, such as vectors, matrices, and data frames. In this article, I will explain how to create and use the frequency tables using different approaches in R with clear examples.
Key Points-
- The
table()
function is used to generate a frequency table from a vector, counting the occurrences of each unique value. - You can create a two-way frequency table by applying the
table()
function to columns of a data frame, which helps in examining the joint distribution of two categorical variables. - The
prop.table()
function converts a frequency table into a relative frequency table, showing the proportion of each category relative to the total. - Continuous data can be grouped into intervals using the
cut()
function, which divides the data into specified ranges, and then thetable()
function can be used to generate a frequency table of these groups. - The
cumsum()
function is used to create a cumulative frequency table, which shows the running total of frequencies, helping to understand the cumulative distribution of the data. - Frequency tables can be sorted in ascending or descending order using the
sort()
function, which is useful for quickly identifying the most or least frequent values. - Frequency tables are visualized using bar plots, which provide a clear graphical representation of the distribution of data.
- The
dplyr
package offers a flexible approach to creating frequency tables, especially within data frames, by using functions likegroup_by()
andsummarise()
.
Create a Frequency Table using table()
Using the base R table() function, you can easily create a frequency table from a single vector. Let’s pass the vector into this function to get a frequency table, which is a list of objects displaying the count of each unique item in the table.
# Create frequency table using table() function
# Create a sample vector
data <- c("A", "B", "C", "D", "E", "F")
print("Given vector:")
print(data)
# Create a table
print("Create a Frequency Table:")
table_data <- table(data)
print(table_data)
Yields below output.
Create Two-Way Frequency Table Using R Data Frame
You can also generate a two-way frequency table using the table()
function. To achieve this, create a data frame with categorical data and pass its columns into the function. This will produce a frequency distribution that shows the relationship between the two variables.
# Create a Two-Way Frequency Table using data frame
# Create a data frame
df = data.frame(col1 = c("A", "B", "C", "D", "E", "F"), col2 = c("X", "Y", "Z", "X", "Y", "Z"))
print("Given data frame:")
print(df)
table_data <- table(df$col1, df$col2)
print("Two-Way Frequency Table:")
print(table_data)
Yields below output.
Plot Frequency Table in R
Alternatively, you can visualize frequency tables with a bar plot, which effectively illustrates the data distribution visually.
# Plot the Frequency Table
barplot(table_data, main = "Frequency of Letters", col = "skyblue", xlab = "Letters", ylab = "Frequency")
Yields below output.
Relative Frequency Table
To convert a frequency table into a relative frequency table, use the prop.table()
function. Simply pass the frequency table into prop.table()
, and it will compute the relative frequencies by dividing each frequency by the total number of observations.
# Create a Relative Frequency Table
table_data <- prop.table(table(data))
print("Relative frequency table:")
print(table_data)
# Output:
# [1] "Relative frequency table:"
# data
# A B C D E F
# 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667
Grouped Frequency Table
A grouped frequency table designed to organize large datasets into ranges, instead of listing each value. You can use the cut()
function to divide the data into intervals, and then apply the table()
function to count how many values fall within each interval.
# Create Grouped Frequency Table
data <- c(55, 72, 88, 91, 45, 60, 78, 83, 95, 67, 73, 88, 92, 54, 61)
# Define the Breaks for Grouping (Class Intervals)
breaks <- c(0, 50, 60, 70, 80, 90, 100)
grouped_data <- cut(data, breaks = breaks, right = FALSE)
# Create grouped frequency table
table_data <- table(grouped_data)
print("Grouped frequency table:")
print(table_data)
# Output:
# [1] "Grouped frequency table:"
# grouped_data
# [0,50) [50,60) [60,70) [70,80) [80,90) [90,100)
# 1 2 3 3 3 3
Cumulative Frequency Table
You can calculate cumulative frequencies from a frequency table using the cumsum()
function. To do this, simply pass the frequency table into the cumsum()
, which will give you the running total of frequencies.
# Create Cumulative Frequency Table
# Create vector
data <- c("A", "B", "C", "D", "E", "F")
# Create a Relative Frequency Table
table_data <- table(data)
print("Create Frequency table:")
print(table_data)
# Create Cumulative Frequency Table
cum_freq_table <- cumsum(table_data)
print("Cumulative Frequency Table:")
print(cum_freq_table)
# Output:
# [1] "Create Frequency table:"
# data
# A B C D E F
# 1 1 1 1 1 1
# [1] "Cumulative Frequency Table:"
# A B C D E F
# 1 2 3 4 5 6
Sort Frequency Table
Similarly, you can manipulate frequency tables by sorting them in ascending or descending order using the sort()
function. To do this, pass the frequency table into sort()
and use the decreasing
parameter. This will sort the frequency values and return them in descending order.
# Sort frequency tables
# Create vector
data <- c("A", "B", "B", "C", "A", "A")
# Create a Relative Frequency Table
table_data <- table(data)
print("Create Frequency table:")
print(table_data)
# Sort the Frequency Table in Descending Order
sorted_table <- sort(table_data, decreasing = TRUE)
print("Sorted Frequency Table:")
print(sorted_table)
# Output:
# [1] "Create Frequency table:"
# data
# A B C
# 3 2 1
# [1] "Sorted Frequency Table:"
# data
# A B C
# 3 2 1
Create a Histogram from the Frequency Table
To create a histogram from a frequency table, you can use the barplot()
function. A histogram provides a graphical representation of the frequency distribution.
# Create a Histogram from the Frequency Table
barplot(table_data, main = "Histogram from Frequency Table", col = "lightgreen", xlab = "Letters", ylab = "Frequency")
Yields below output.
Frequency Table in R with dplyr
Finally, you can use the dplyr
package to create frequency tables within a data frame, which is particularly useful when working with larger datasets.
# Using dplyr to create a frequency table
library(dplyr)
# Create a Data Frame
df <- data.frame(Letters = data)
# Frequency Table with dplyr
table_data <- df %>%
group_by(Letters) %>%
summarise(Frequency = n())
print(" Create frequency table:")
print(table_data)
# Output:
[1] " Create frequency table:"
# # A tibble: 6 × 2
# Letters Frequency
# <chr> <int>
# 1 A 1
# 2 B 1
# 3 C 1
# 4 D 1
# 5 E 1
# 6 F 1
Conclusion
In this article, I have explained frequency tables in R provide an essential method for summarizing and analyzing categorical data. By using the table()
function and related tools, you can create and implement simple and complex frequency tables, visualize distributions, and perform deeper data analysis.
Happy Leraning!!