How to extract the dataframe row with min or max values in R ?
Last Updated :
09 Dec, 2022
The tabular arrangement of rows and columns to form a data frame in R Programming Language supports many ways to access and modify the data. Application of queries and aggregate functions, like min, max and count can easily be made over the data frame cell values. Therefore, it is relatively very easy to access a subset of the data frame based on the values contained in the cell.
Example 1: Determining the row with min or max value based on the entire data frame values.
An iteration is made over the data frame cells, by using two loops for each row and column of the data frame respectively. The cell value is compared to the initial minimum and maximum values respectively and updated in case the value satisfies the constraint. Also, a variable is declared to keep the current row index satisfying the condition. Then, the row at this index of the data frame is accessed. Time complexity is polynomial with respect to the size of the data frame.
R
# declaring a data frame in R
data_frame = data.frame(C1 = c(5:8),
C2 = c(1:4),
C3 = c(9:12),
C4 = c(13:16))
print("Original data frame")
print(data_frame)
# declaring initial values for min and max
min = 32767
max = -32767
min_row_indx = 0
max_row_indx = 0
# looping over the data frame values
for (i in 1:nrow(data_frame)){
# for-loop over columns
for(j in 1:ncol(data_frame)) {
# checking if the dataframe
# cell value is less than minimum
if(data_frame[i,j]<min){
# replacing the minimum
# with the smaller value
min = data_frame[i,j]
# updating the row with the
# smallest value found until now
min_row_indx = i
}
# checking if the data frame
# cell value is more than maximum
if(data_frame[i,j]>max){
# replacing the minimum
# with the smaller value
max = data_frame[i,j]
# updating the row with the
# smallest value found until now
max_row_indx = i
}
}
}
# printing the row with minimum value
print ("Row with minimum value in the data frame")
print (data_frame[min_row_indx,])
# printing the row with maximum value
print ("Row with maximum value in the data frame")
print (data_frame[max_row_indx,])
Output:
[1] "Original data frame"
C1 C2 C3 C4
1 5 1 9 13
2 6 2 10 14
3 7 3 11 15
4 8 4 12 16
[1] "Row with minimum value in the data frame"
C1 C2 C3 C4
1 5 1 9 13
[1] "Row with maximum value in the data frame"
C1 C2 C3 C4
4 8 4 12 16
Example 2: Determining the row with min or max value based on a data frame column
The function which.min() in R can be used to compute the minimum of all the values in the object specified as argument, whether it be a list, matrix, or data frame. Similarly, which.max() computes the largest of all the values. In order to select a particular column of the data frame we use df$colname and then apply this as an index of the data frame to extract the complete row with the specified aggregate function. This approach can be applied to all the data types, numeric, string as well as factor. The time complexity required is linear with respect to the column length since we directly access and compare all the values of this particular column.
The following syntax in R is used to extract the row with minimum or maximum value in the column specified in the argument :
Syntax: df[which.min(df$colname),]
Arguments :
- df - Data Frame to extract the minimum or maximum value from
- colname - Column name to consider calculating minimum or maximum from.
Returns : The row with the maximum or minimum cell value in the particular column.
Code:
R
# declaring a data frame in R
data_frame = data.frame(C1 = c(1:4),
C2 = c( 5:8),
C3 = c(9:12),
C4 = c(13:16))
print("Original data frame")
print(data_frame)
# extracting the row with
# maximum value in C2 column
print ("Row with max C2 value")
data_frame[which.max(data_frame$C2),]
# extracting the row with
# minimum value in C4 column
print ("Row with min C4 value")
data_frame[which.min(data_frame$C4),]
Output:
[1] "Original data frame"
C1 C2 C3 C4
1 1 5 9 13
2 2 6 10 14
3 3 7 11 15
4 4 8 12 16
[1] "Row with max C2 value"
C1 C2 C3 C4
4 4 8 12 16
[1] "Row with min C4 value"
C1 C2 C3 C4
1 1 5 9 13
In case the data frame contains string type variable values, the minimum and maximum are computed upon sorting this data lexicographically.
R
# declaring a data frame in R
data_frame = data.frame(C1= c("a","b","c","d"),
C2= c("geeks","dataframe","in","R"),
C3= c(9:12),C4=c(13:16))
print("Original data frame")
print(data_frame)
# extracting the row with maximum value in
# C2 column
print ("Row with max C1 value")
data_frame[which.max(data_frame$C1),]
# extracting the row with minimum value in
# C4 column
print ("Row with min C2 value")
data_frame[which.min(data_frame$C2),]
Output
[1] "Original data frame"
C1 C2 C3 C4
1 a geeks 9 13
2 b dataframe 10 14
3 c in 11 15
4 d R 12 16
[1] "Row with max C1 value"
C1 C2 C3 C4
4 d R 12 16
[1] "Row with min C2 value"
C1 C2 C3 C4
2 b dataframe 10 14
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read