0

I am trying to extract the team with the maximum number of wins each year in women's college basketball, and I am currently stuck with having the number of wins for each year for each team, and I want only the team with the maximum number of wins in each year.

winsbyyear <- WomenCBnewdf %>%
  group_by(Year,Team)%>%
  summarise(totalwinsyr = sum(Outcome))

Output currently looks like this, but I am expecting to see each year only once with the team with the maximum number of wins in the subsequent columns

Year  Team              totalwinsyr
   <fct> <chr>                   <dbl>
 1 2014  AbileneChristian           10
 2 2014  AirForce                    0
 3 2014  Akron                      18
 4 2014  Alabama                    10
 5 2014  AlabamaAM                   3
 6 2014  AlabamaHuntsville           0
 7 2014  AlabamaMobile               0
 8 2014  AlabamaSt                  15
 9 2014  AlaskaAnchorage             1
10 2014  AlbanyNY                   16

How to select the rows with maximum values in each group with dplyr?

I have already looked here but I could not find any resources to help with a group_by() with multiple values

Ptarala99
  • 23
  • 1
  • 1
  • 4
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It would be better to include more than one year in the sample data so we can test for that condition. Also please share data via a `dput()` other other copy/paste-into-R friendly format. Sounds like you just need a filter after the summarize. – MrFlick Feb 22 '22 at 23:40
  • 3
    If I understand correctly, it looks like the solution is in the question you link. It looks like your data would still be grouped by `Year` alone from the code you provided, so it's just `winsbyyear %>% filter(totalwinsyr == max(totalwinsyr))` – TrainingPizza Feb 22 '22 at 23:42

1 Answers1

1

Create a new column with the number of wins and then filter:

winsbyyear <- WomenCBnewdf %>%
  group_by(Year,Team)%>%
  mutate(totalwinsyr = sum(Outcome)) %>%
  filter(totalwinsyr == max(totalwinsyr))
Vincent
  • 15,809
  • 7
  • 37
  • 39