1

I have a df that has three columns

day       city    value
Monday     Rio    100
Tuesday    Rio    90
Wednesday  Rio    80
Thursday   Rio    96
Friday     NY     300
Saturday   NY     500
Sunday     NY     600
Monday     NY     100
Tuesday    NY     20
Wednesday  LA     50
Thursday   LA     60
Friday     LA     70
Saturday   LA     80
 ...       ...    ...

DESIRED DATAFRAME

day       city    value
Monday     Rio    100
Thursday   Rio    96
Saturday   NY     500
Sunday     NY     600
Friday     LA     70
Saturday   LA     80
...        ...    ...

The 2 largest values of each city and of course, the day column

stefan
  • 90,330
  • 6
  • 25
  • 51
Fernanda
  • 313
  • 1
  • 6
  • 1
    Possible duplicates: https://stackoverflow.com/questions/24558328/select-the-row-with-the-maximum-value-in-each-group and https://stackoverflow.com/questions/57224991/select-top-rows-in-r-using-add-tally-and-top-n-functions – stefan Jun 03 '21 at 13:56
  • Thanks!! df %>% group_by(city) %>% top_n(2, value) – Fernanda Jun 03 '21 at 13:59

1 Answers1

0

Here is how I would do it:

library(dplyr)

your_data_set %>%
  split(~ city) %>%
  purrr::map(~ head(arrange(., desc(value)), 2)) %>%
  bind_rows()
ktiu
  • 2,606
  • 6
  • 20