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
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)
min = 32767
max = -32767
min_row_indx = 0
max_row_indx = 0
for (i in 1: nrow (data_frame)){
for (j in 1: ncol (data_frame)) {
if (data_frame[i,j]<min){
min = data_frame[i,j]
min_row_indx = i
}
if (data_frame[i,j]>max){
max = data_frame[i,j]
max_row_indx = i
}
}
}
print ( "Row with minimum value in the data frame" )
print (data_frame[min_row_indx,])
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
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)
print ( "Row with max C2 value" )
data_frame[ which.max (data_frame$C2),]
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
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)
print ( "Row with max C1 value" )
data_frame[ which.max (data_frame$C1),]
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
How to find the mean of all values in an R data frame?
In this article, we are going to find the mean of the values of a dataframe in R with the use of mean() function. Syntax: mean(dataframe) 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 the elements and
2 min read
Select First Row of Each Group in DataFrame in R
In this article, we will discuss how to select the first row of each group in Dataframe using R programming language. The duplicated() method is used to determine which of the elements of a dataframe are duplicates of other elements. The method returns a logical vector which tells which of the rows
2 min read
How to Remove Rows with Some or All NAs in R DataFrame?
In this article, we will discuss how to remove rows with some or all NA's in R Programming Language. We will consider a dataframe and then remove rows in R. Let's create a dataframe with 3 columns and 6 rows. C/C++ Code # create dataframe data = data.frame(names=c("manoj", "bobby
3 min read
Find the index of the maximum value in R DataFrame
In this article, we will see how to find the index of the maximum value from a DataFrame in the R Programming Language We can find the maximum value index in a dataframe using the which.max() function. Syntax: which.max(dataframe_name$columnname) "$" is used to access particular column of a datafram
2 min read
Extract first N rows from dataframe in R
A required number of rows can be retrieved from a dataframe for some computation that demands so. In this article, we will be discussing two different approaches for extracting the first n rows from the data frame File in Use: Method 1: Using head() function This function will help to extract the gi
1 min read
Select rows from a DataFrame based on values in a vector in R
In this article, we will discuss how to select rows from a DataFrame based on values in a vector in R Programming Language. Method 1: Using %in% operator %in% operator in R, is used to identify if an element belongs to a vector or Dataframe. It is used to perform a selection of the elements satisfyi
5 min read
How to check missing values in R dataframe ?
In this article, we are going to see how to find out the missing values in the data frame in R Programming Language. Approach: Step 1: Create DataFrame. Let us first create a data frame with some missing values and then demonstrate with an example how to find the missing values. C/C++ Code data <
2 min read
How to Delete Row(s) in R DataFrame ?
In this article, we will see how row(s) can be deleted from a Dataframe in R Programming Language. Deleting a single row For this, the index of the row to be deleted is passed with a minus sign. Syntax: df[-(index), ] Example 1 : C/C++ Code # creating a data frame with # some data . df=data.frame(id
2 min read
Extract vector from dataframe in R
In this article, we will see how to extract vectors from DataFrame in R Programming Language. The approach is simple, by using $ operator, we can convert dataframe column to a vector. Syntax: dataframe_name$columnname Given below are various examples to implement the same Example 1: C/C++ Code # cre
1 min read
Reshape DataFrame from Long to Wide Format in R
In this article, we will discuss how to reshape dataframe from long to wide format in R programming language. The data in the dataframe is recognized by the format in which the storage and retrieval happens. Duplicate key-value pairs exist in dataframe and can be rearranged using the following metho
3 min read