How to return a value corresponding to another variable's max/min among groups?

Dear RStudio Community,

Here is a look of my data structure:

What I need to do: "show and comment for each year from 1995 (included):
the name of the movie with the highest total gross and the value of this gross"

For so far, what I can do is:
mayor_recaudacion <- movies_century %>%
group_by(Released) %>%
filter(Released>=1995) %>%
select(Released, Movie, Worldwide_Gross) %>%
summarize(max = max(Worldwide_Gross))

Which results look like that:

   Released    max
  1. "1995" "366101666"

  2. "1996" "817400878"

  3. "1997" "139801096"

So I need to return the corresponding Movie name for each max Gross found, in the same table but I don't know how to compute it.

Any ideas ? :slight_smile: Don't hesitate to ask for clarification if my request is not clear!

Thank you!

You could filter instead of summarizing, something like this

filter(Worldwide_Gross == range(Worldwide_Gross)[2])

if you need more specific help, please provide a proper REPRoducible EXample (reprex)

Perfect, it works :slight_smile: Thank you!

If I may ask another question: is it possible to use the function row_number() in this case?

Sorry but I don't understand your question, you want to use it for what purpose?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.