Open In App

Frequency table in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to make a frequency table in the R programming language.

Frequency Table

A frequency table is a list of objects with the frequency of each item shown in the table.  When evaluating categorical data to determine how frequently a variable appears in their data set, statisticians frequently utilize a frequency table. We will frequently encounter frequency distribution tables if we intend to work as data science analysts.

One-Way Frequency Tables in R

We have a one-way Frequency table in R Programming Language so we will study this step by step and with multiple methods.

Method 1:Create Frequency Table in base R

In this method, we will be simply using the table() function from the base R, where we will be simply passing data as its parameter to the function and this function will further generate the frequency table.

table() function:

table() function in R Language is used to create a categorical representation of data with the variable name and frequency in the form of a table.

   Syntax: table(x)

   Parameters:

   x: Object to be converted

Example:

In this example, we will be generating a frequency table using the table() function and using that table plotting bar plot in the R programming language. 

R




# Create Data
data<-c('G','E','E','T','A',
'N','S','H','S','A','H','N','I')
 
# Use table() to get the frequency table
table <- table(data)
# Printing table
 
print(table)
# Use barplot to visualize
# a frequency table in a graphic
barplot(table)


Output:

data
A E G H I N S T
2 2 1 2 1 2 2 1
Frequency table in R-Geeksforgeeks

Frequency table in R

As we can see in the output frequency table “A”, “E”, “H”, “N”, and “N” occurs two times in our data set rest of the letters occurs only one time and that data is representing in the form of bar plot.

Method 2:Create Frequency Table with Proportions

In this method, we will be creating a frequency table with proportions using the sum() function with the table() function, here table() function will simply create the frequency table, and the sum() function will be getting the sum, of all values which will be further be divided with the initial values of the table to get the values as per the proportionality. 

sum() function:

sum() function in R Programming Language returns the addition of the values passed as arguments to the function.

   Syntax: sum(…)

   Parameters:

  • …: numeric or complex or logical vectors

Example:

In this example, we have used the same data as used in the previous example, further, we are using the sum() function to get the sum of the values and further will be getting the frequency table with Proportions in the R programming language.

R




# Create Data
data<-c('G','E','E','T','A',
 'N','S','H','S','A','H','N','I')
 
# Use table() to get the frequency table
table<-table(data)
 
# Use sum() function to
# Create Frequency Table with Proportions
prob_table <- table / sum(table)     
print(prob_table)


Output:

data
A E G H I N S
0.15384615 0.15384615 0.07692308 0.15384615 0.07692308 0.15384615 0.15384615
T
0.07692308

Method 3:Create Cumulative Frequency Table

In this method, we will be creating a Cumulative frequency table with the table() function, here table function will simply create the frequency table, and using cumsum() function we will be getting the Cumulative sum of all values and setting it into the table.

cumsum() function:

cumsum() function in R programming language is used to calculate the cumulative sum of the vector passed as an argument.

Syntax: cumsum(x)

Parameters:

x: Numeric Object

Example:

In this example, we have used the same data as used in the previous example, further, we are using the cumsum() function by passing the table as its parameter and further, we will be getting the cumulative frequency table in the R programming language.

R




# Create Data
data<-c('G','E','E','T','A',
        'N','S','H','S','A','H','N','I')
 
# Use table() to get the frequency table
table<-table(data)
print("Simple Frequency Table")
print(table)
# Use cumsum function to
# Create cumulative frequency table
cumsum_table <- cumsum(table)  
print("cumulative Frequency Table")
print(cumsum_table)


Output:

[1] "Simple Frequency Table"
data
A E G H I N S T
2 2 1 2 1 2 2 1
[1] "cumulative Frequency Table"
A E G H I N S T
2 4 5 7 8 10 12 13

Two-Way Frequency Tables in R

Two-way frequency tables, also known as contingency tables, are essential tools in data analysis when we want to explore the relationships between two categorical variables.

R




# Set seed for reproducibility
set.seed(50)
 # Create data frame
data <- data.frame(
  employee = c('A', 'B', 'A','A', 'B', 'C','A','B','C'),
  sales = round(runif(9, 2000, 5000), 0),
  complaints = c('Yes','No','Yes','Yes','Yes','Yes','No','No','Yes') )
# print data
print(data)


Output:

  employee sales complaints
1 A 4126 Yes
2 B 3313 No
3 A 2600 Yes
4 A 4301 Yes
5 B 3539 Yes
6 C 2134 Yes
7 A 4100 No
8 B 3939 No
9 C 2126 Yes

Calculate two-way frequency table

R




table(data$employee,data$complaints)


Output:

   
No Yes
A 1 3
B 2 1
C 0 2

Plot a bar chart to view the two wat frequency table

R




# Load necessary libraries (ggplot2 for plotting)
library(ggplot2)
 
# Create a table for the data
table_data <- table(data$employee, data$complaints)
table_data <- as.data.frame(table_data)
 
# Create a stacked bar plot
ggplot(table_data, aes(x = Var1, y = Freq, fill = Var2)) +
  geom_bar(stat = "identity") +
  labs(
    title = "Employee Complaints",
    x = "Employee",
    y = "Count"
  ) +
  scale_fill_manual(values = c("No" = "blue", "Yes" = "red")) +  # Custom color palette
  theme_minimal()


Output:

Frequency table in R-Geeksforgeeks

Frequency table in R

Calculate total sales of employee using group_by()

We can calculate the total sales of employee using group_by function so we will get totalsales of all employees.

R




# Load the dplyr library
library(dplyr)
 
# Group the data by employee and calculate the total sales for each employee
total_sales <- data %>%
  group_by(employee) %>%
  summarize(total_sales = sum(sales))
 
# Print the result
total_sales


Output:

# A tibble: 3 × 2
employee total_sales
<chr> <dbl>
1 A 15127
2 B 10791
3 C 4260



Last Updated : 25 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads