How to calculate the mode of all rows or columns from a dataframe in R ?
Last Updated :
22 Feb, 2023
In this article, we will discuss how to calculate the mode of all rows and columns from a dataframe in R Programming Language.
Method 1: Using DescTools package
The DescTools package in R is used to perform descriptive analysis. It contains a collection of miscellaneous basic statistic functions and convenience wrappers for efficiently describing data. It can be installed into the R working space using the following syntax :
install.packages("DescTools")
The mode() method of this package is used to return the most frequently occurring numeric or character value from the input vector.
Syntax: Mode(vec, na.rm = FALSE)
Arguments :
vec - a (non-empty) numeric vector of values.
na.rm (Default : false)- Indicator of whether the missing values should be removed.
In this approach, a for loop is initiated for iterating over all the columns, and then each individual column is supplied as an individual vector in the Mode() method.
Code:
R
library ("DescTools")
# declaring a dataframe
data_frame = data.frame(col1 = c("b", "b", "d", "e", "e") ,
col2 = c(0, 2, 1, 2, 5),
col3= c(TRUE, FALSE, FALSE,
TRUE, TRUE))
print ("Original dataframe")
print (data_frame)
print ("Mode of columns \n")
# iterating over all the columns of the
# dataframe
for (i in 1:ncol(data_frame)){
# calculating mode of ith column
mod_val <- Mode(data_frame[,i])
cat(i, ": ",mod_val,"\n")
}
Output:
[1] "Original dataframe"
col1 col2 col3
1 b 0 TRUE
2 b 2 FALSE
3 d 1 FALSE
4 e 2 TRUE
5 e 5 TRUE
[1] "Mode of columns"
1 : 1 3
2 : 2
3 : TRUE
In the previous example, the numeric equivalent to the col1 is returned to the mode values. This leads to ambiguity or loss of data. In order to remove this problem, the explicit conversion to as.character() can be done.
R
library ("DescTools")
# declaring a dataframe
data_frame = data.frame(col1 = c("b","b","d","e","e") ,
col2 = c(0,2,1,2,5),
col3= c(TRUE,FALSE,FALSE,TRUE, TRUE))
print ("Original dataframe")
print (data_frame)
print ("Mode of columns \n")
# iterating over all the columns
# of the dataframe
for (i in 1:ncol(data_frame)){
# calculating mode of ith column
mod_val <- as.character(Mode(data_frame[,i]))
cat(i, ": ",mod_val,"\n")
}
Output:
[1] "Original dataframe"
col1 col2 col3
1 b 0 TRUE
2 b 2 FALSE
3 d 1 FALSE
4 e 2 TRUE
5 e 5 TRUE
[1] "Mode of columns"
1 : b e
2 : 2
3 : TRUE
Method 2: User-defined method
A for loop iteration is done over all the columns of the dataframe. The mode can be calculated using the user-defined function by the following steps :
Step 1: Compute the unique values of the vector using the unique() method in R. It returns the unique values from the vector.
Step 2: Match method is called to return a vector of the positions of (first) matches of its first specified argument in its second argument. The first vector is the original column vector and the second is the unique vector.
match (col , unique_vec)
Step 3: The tabulate() method is then invoked which takes as the input the matched integer-valued vector and counts the number of occurrences of each integer in the specified vector.
Step 4: The max value out of these tabulated values is then calculated using the max() method, which is then returned as the mode of the column.
Code:
R
# create function to compute mode
mode <- function(x) {
# function to compute unique values
# in vector
unq_data <- unique(x)
# map values to its number of occurrences
map_data <- match(x, unq_data)
# table of the data with its values
tabulate_data <- tabulate(map_data)
# compute maximum value from data
max_val <- max(tabulate_data)
# plot it from table
unq_data[tabulate_data == max_val]
}
# declaring a dataframe
data_frame = data.frame(col1 = c("b","b","d","e","e") ,
col2 = c(0,2,1,2,5),
col3= c(TRUE,FALSE,FALSE,TRUE, TRUE))
print ("Original dataframe")
print (data_frame)
print ("Mode of columns \n")
# iterating over all the columns of
# the dataframe
for (i in 1:ncol(data_frame)){
# calculating mode of ith column
mod_val <- mode(data_frame[,i])
print (mod_val)
}
Output:
[1] "Original dataframe"
col1 col2 col3
1 b 0 TRUE
2 b 2 FALSE
3 d 1 FALSE
4 e 2 TRUE
5 e 5 TRUE
[1] "Mode of columns"
[1] b e
Levels: b d e
[1] 2
[1] TRUE
Similar Reads
How to get the classes of all columns in a dataframe in R ?
In this article, we will discuss how to find all the classes of the dataframe in R Programming Language. There are two methods to find the classes of columns in the dataframe. Using str() functionUsing lapply() function Method1 : Using str() function This function will return the class and value of
2 min read
How to convert DataFrame column from Character to Numeric in R ?
In this article, we will discuss how to convert DataFrame column from Character to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type c
5 min read
Select Odd and Even Rows and Columns from DataFrame in R
In this article, we will discuss how to select odd and even rows from a dataframe in R programming language. Getting Odd Rows from the Data Frame The number of rows in a data frame in R can be fetched by the nrow() method. It returns the number of rows in the data frame. The seq_len() method is then
6 min read
Calculate mean of multiple columns of R DataFrame
Mean is a numerical representation of the central tendency of the sample in consideration. In this article, we are going to calculate the mean of multiple columns of a dataframe in R Programming Language. Formula: Mean= sum of observations/total number of observations. Method 1: Using colMeans() fun
2 min read
How to find the sum of column values of an R dataframe?
In this article, we are going to find the sum of the column values of a dataframe in R with the use of sum() function. Syntax: sum(dataframe$column_name) Creating a Dataframe A dataframe can be created with the use of data.frame() function that is pre-defined in the R library. This function accepts
2 min read
How to find Mean of DataFrame Column in R ?
In this article, we will discuss how to compute the mean of the Dataframe Column in R Programming language. It can be done in various ways: Using $-OperatorUsing [[]]Using Column IndexUsing summarise function of the dplyr PackageUsing colMeans Function Method 1: Using $-Operator. This is one of the
5 min read
How to assign column names based on existing row in R DataFrame ?
In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language. Method 1 : Using as.character() method unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all componen
3 min read
How to convert dataframe columns from factors to characters in R?
In this article, we will discuss how to convert dataframe columns from factors to characters in R Programming Language. A dataframe can have different types of columns stacked together to form a tubular structure. Easy modification of the columns' data as well as conversion between data types can be
5 min read
Find columns and rows with NA in R DataFrame
A data frame comprises cells, called data elements arranged in the form of a table of rows and columns. A data frame can have data elements belonging to different data types as well as missing values, denoted by NA. Approach Declare data frameUse function to get values to get NA valuesStore positio
3 min read
How to Add an Empty Column to DataFrame in R?
In this article, we will discuss how to add an empty column to the dataframe in R Programming Language. Add One Empty Column to dataframe Here we are going to add an empty column to the dataframe by assigning column values as NA. Syntax: dataframe[ , 'column_name'] = NA where, dataframe  is the inpu
1 min read