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
  • 163,977
  • 32
  • 423
  • 453
user1997567
  • 409
  • 3
  • 16

1 Answers1

2

You can try

df %>% 
  group_by(State, Year, Sex) %>% 
  slice(which.max(Percent)) 
talat
  • 66,143
  • 20
  • 123
  • 153