Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • Comparison between Statistics and Luck
    Lottery Prediction-Comparison between Statistics and Luck Machine Learning
  • Credit Card Fraud detection in R
    Credit Card Fraud Detection in R R
  • 5 Free Books to Learn Statistics For Data Science
    5 Free Books to Learn Statistics For Data Science Course
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • Top 10 online data science programmes
    Top 10 online data science programs Course
  • Applications of Data Science in Education
    Applications of Data Science in Education Machine Learning
  • best books about data analytics
    Best Books to Learn Statistics for Data Science Course
Find the Maximum Value by Group in R

Find the Maximum Value by Group in R

Posted on June 28June 26 By Jim No Comments on Find the Maximum Value by Group in R
Tweet
Share
Share
Pin

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

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: How to Group and Summarize Data in R
Next Post: Replace NA with Zero in R

Related Posts

  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • Error in solve.default(mat)  Lapack routine dgesv system is exactly singular
    Error in solve.default(mat) :  Lapack routine dgesv: system is exactly singular: U[2,2] = 0 R
  • What is bias variance tradeoff
    What is the bias variance tradeoff? R
  • How to Avoid Overfitting
    How to Avoid Overfitting? Machine Learning
  • How do confidence intervals work
    How do confidence intervals work? R
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • glm function in R
    glm function in r-Generalized Linear Models R
  • Beginner's Guide to Data Science
    Beginner’s Guide to Data Science Machine Learning
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • Comparing group means in R
    One way ANOVA Example in R-Quick Guide R
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • best books about data analytics
    Best Books About Data Analytics Course
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme