0

Dataframe has the columns:

State Sex Year Name Number Percent

I need to filter for each year, one male and one female with highest percentage, in every state.

Example:

Washington  M   2011   John    34   0.46
Washington  F   2011   Mary    42   0.67


Washington M   2012   John    46   0.46
Washington F   2012   Mary    64   0.67

and so on for every State and year.

joran
  • 169,992
  • 32
  • 429
  • 468
user1997567
  • 439
  • 4
  • 19

1 Answers1

2

You can try

df %>% 
  group_by(State, Year, Sex) %>% 
  slice(which.max(Percent)) 
talat
  • 68,970
  • 21
  • 126
  • 157