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

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

Frequency table in R

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

Table of Content

  • Frequency Table
  • One-Way Frequency Tables in R
  • Two-Way Frequency Tables in R

Similar Reads

Frequency Table

...

One-Way Frequency Tables in R

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....

Two-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....

Contact Us