Find the Maximum Value by Group in R

Find the Maximum Value by Group in R, you may frequently want to determine the highest value for each group in a data frame. Fortunately, utilizing the dplyr package’s methods makes this task simple.

Interactive 3d plot in R-Quick Guide – Data Science Tutorials

The following data frame is used in this tutorial to demonstrate how to achieve that.

Let’s create a data frame

df <- data.frame(team = c('T1', 'T1', 'T1', 'T2', 'T2', 'T2', 'T2'),
                 position = c('R1', 'R2', 'R1', 'R1', 'R1', 'R1', 'R2'),
                 points = c(122, 135, 129, 322, 334, 434, 139))

Let’s view the data frame

df
team position points
1   T1       R1    122
2   T1       R2    135
3   T1       R1    129
4   T2       R1    322
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

Example 1: Find Max Value by Group

The maximum value for team and position can be found using the code below.

Arrange Data by Month in R with example – Data Science Tutorials

library(dplyr)

To find the maximum value by team and position

df %>%
  group_by(team, position) %>%
  summarise(max = max(points, na.rm=TRUE))
team  position   max
  <chr> <chr>    <dbl>
1 T1    R1         129
2 T1    R2         135
3 T2    R1         434
4 T2    R2         139

Example 2: Retrieve Rows with Max Value by Group

The code below can be used to find the maximum value for the team and position.

How to add columns to a data frame in R – Data Science Tutorials

library(dplyr)

To locate rows with the highest number of points by team and position

df %>%
  group_by(team, position) %>%
  filter(points == max(points, na.rm=TRUE))
team  position points
  <chr> <chr>     <dbl>
1 T1    R2          135
2 T1    R1          129
3 T2    R1          434
4 T2    R2          139

Example 3: Return a Single Row with the Maximum Value for the Group

In the preceding illustration, team A had two players in positions G who each had the maximum number of points.

Create new variables from existing variables in R – Data Science Tutorials

Use the slice() function as follows if you just want to retrieve the first player in a group with the maximum value.

df %>%
  group_by(team, position) %>%
  slice(which.max(points))
team  position points
  <chr> <chr>     <dbl>
1 T1    R1          129
2 T1    R2          135
3 T2    R1          434
4 T2    R2          139

You may also like...

Leave a Reply

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

3 × one =