Find the Maximum Value by Group in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Find the Maximum Value by Group in R appeared first on Data Science Tutorials

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

The post Find the Maximum Value by Group in R appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)