Home > Mobile >  Find max value from r combined with other columns
Find max value from r combined with other columns

Time:09-28

I have a Dataframe called swimming that looks as following:

Sport                  Time
50 meter swimming      20,23
50 meter swimming      21,43
75 meter swimming      35,43
75 meter swimming      36,43
100 meter swimming     45,43
100 meter swimming     43,23
125 meter swimming     57,42
125 meter swimming     54,32

What I'm trying to figure out is to how to find the min value of every swimming distance so a Dataframe results in the following tibble:

Sport                  Time
50 meter swimming      21,43
75 meter swimming      35,43
100 meter swimming     43,23
125 meter swimming     54,32

What I have now is the following code which results in the tibble below.

swimming %>%
group_by(Sport, Time) %>%
slice_min(Time)
Sport                  Time
50 meter swimming      20,23
50 meter swimming      21,43
75 meter swimming      35,43
75 meter swimming      36,43
100 meter swimming     43,23
100 meter swimming     45,43
125 meter swimming     54,32
125 meter swimming     57,42

Is there any code that works as the slice_min but not include all rows?

CodePudding user response:

Remove Time from your group_by().

Currently, you are creating a separate group for each sport time combination—-which is every row. You just want a group for each sport.

  •  Tags:  
  • r
  • Related