Hello, readers! In this article, we will be focusing on Frequency Table in R programming in detail. So, let us get started!!
Use of a Frequency table in R
In the terms of statistics, the minimal action which we perform is the analysis of data in terms of statistical values.
On similar lines, in the domain of data science and machine learning, the first and foremost task is to analyze and study the data in terms of the kind of data values represented.
This is when we can make use of Frequency Table.
As we all know that data variables are of two kinds: continuous and categorical.
So, my question is, how would anyone understand the number of values represented by every group of the categorical variable?
Ok, here I go with the solution 🙂
We can create a
Frequency Table that would represent the count of data values represented by every group of the category. By this, we can maintain the count of every category of the dataset easily.
In R programming, we make use of
table() function to create a table of the of the frequency of every group of the variable.
Thus, in a Nutshell, the table() function enables us to create a table of the data values of every variable along with the frequency count of the same.
Have a look at the below syntax!
We need to pass the data to the function for it to return the frequency table of the same.
As a result, it returns a table of the data values and not a data frame!
Recommended Read – Tidyr Library in R
Example 1: table() function with single list of values
In the below example, we have created a list of values as a 1-D values and passed it to the function.
In return, we get a table that has the frequency(count) of the values below them.
#Removed all the existing objects rm(list = ls()) data = c(10,20,30,40,50,10,20,30,30,50) tab_data = table(data) print(tab_data)
> print(tab_data) data 10 20 30 40 50 2 2 3 1 2
Example 2: table() function with two lists as arguments
Now, instead of passing a single list of values, we have created two lists and then have passed them to the function.
#Removed all the existing objects rm(list = ls()) data = c(10,20,30,40,50,10,20,30,30,50) info = c(10,10,10,10,20,30,50,40,30,60) tab_data = table(data,info) print(tab_data)
So, as a result, the table() function would create a table of NxN elements of the lists and represent the frequency of every value against it.
> print(tab_data) info data 10 20 30 40 50 60 10 1 0 1 0 0 0 20 1 0 0 0 1 0 30 1 0 1 1 0 0 40 1 0 0 0 0 0 50 0 1 0 0 0 1
Example 3: table() function with the columns of dataset
In this example, we have used the Bike Rental Prediction dataset. You can find the dataset here.
As the table() function is best suited for categorical data, we calculate the frequency table for ‘weekday’ and ‘yr’, respectively.
#Removed all the existing objects rm(list = ls()) #Setting the working directory setwd("D:/Ediwsor_Project - Bike_Rental_Count/") getwd() #Load the dataset bike_data = read.csv("day.csv",header=TRUE) tab_data = table(bike_data$weekday,bike_data$yr) print(tab_data)
> print(tab_data) 0 1 0 52 53 1 52 53 2 52 52 3 52 52 4 52 52 5 52 52 6 53 52
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to R programming, stay tuned!
Till then, Happy Learning 🙂