-2

My data has continent, country and dif(number).I am now trying to find the maximum of dif by each continent, and following is my code. How can I get the countries name at the same time?

dt_dif %>%
  group_by(continent)%>%
  summarize(max_dif = max(dif))
Junhui Wu
  • 11
  • 3
  • Try to make this a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)—we can't run your code without your data, and can't see any of what you're working with or what this code gets you. You want the row with the maximum value per continent? – camille Jan 29 '20 at 02:47

1 Answers1

-2

We can use slice to return the row of the dataset with the max value of 'dif'

library(dplyr)
dt_dif %>%
    group_by(continent) %>%
    slice(which.max(dif))

Or using filter

dt_dif %>%
    group_by(continent) %>%
    filter(dif == max(dif))
akrun
  • 874,273
  • 37
  • 540
  • 662