R: How to Select Row with Max Value in Specific Column


Often you may want to select the row with the max value in a specific column of a data frame in R.

You can use the following methods to do so:

Method 1: Select First Row with Max Value Using Base R

df[which.max(df$assists),]

This method will return the first row with the max value in the assists column of the data frame.

Method 2: Select All Rows with Max Value Using Base R

df[df$assists == max(df$assists),]

This method will return all rows with the max value in the assists column of the data frame.

Method 3: Select First Row with Max Value Using dplyr

library(dplyr)

df %>% slice_max(assists) %>% slice(1)

This method will return the first row with the max value in the assists column of the data frame.

Method 4: Select All Rows with Max Value Using dplyr

library(dplyr)

df %>% slice_max(assists)

This method will return all rows with the max value in the assists column of the data frame.

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 31, 35, 34, 45, 28, 45),
                 rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#specify row numbers
rownames(df) <- c(4, 3, 7, 6, 1, 2, 8, 5)

#view data frame
df

  team points assists rebounds
4    A     99      22       30
3    A     68      28       28
7    A     86      31       24
6    A     88      35       24
1    B     95      34       30
2    B     74      45       36
8    B     78      28       30
5    B     93      45       29

Let’s jump in!

Example 1: Select First Row with Max Value Using Base R

We can use the following syntax in base R to return the first row with the max value in the assists column of the data frame:

#return first row with max value in the assists column
df[which.max(df$assists),]

  team points assists rebounds
6    B     74      45       36

There are two rows that have the max value (45) in the assists column of the data frame and this method returns only the first row.

Example 2: Select All Rows with Max Value Using Base R

We can use the following syntax in base R to return all rows with the max value in the assists column of the data frame:

#return all rows with max value in the assists column
df[df$assists == max(df$assists),]

  team points assists rebounds
6    B     74      45       36
8    B     93      45       29

There are two rows that have the max value (45) in the assists column of the data frame and this method returns both rows.

Example 3: Select First Row with Max Value Using dplyr

We can use the following syntax in dplyr to return the first row with the max value in the assists column of the data frame:

library(dplyr)

#select first row with max value in assists column
df %>% slice_max(assists) %>% slice(1)

  team points assists rebounds
1    B     74      45       36

There are two rows that have the max value (45) in the assists column of the data frame and this method returns only the first row.

Example 4: Select All Rows with Max Value Using dplyr

We can use the following syntax in dplyr to return all rows with the max value in the assists column of the data frame:

library(dplyr)

#select all rows with max value in assists column
df %>% slice_max(assists)

  team points assists rebounds
1    B     74      45       36
2    B     93      45       29

There are two rows that have the max value (45) in the assists column of the data frame and this method returns both rows.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Retrieve Row Numbers in R
How to Replace NAs with Strings in R
How to Impute Missing Values in R

Leave a Reply

Your email address will not be published. Required fields are marked *