0

I have the following dataset:

Fitness Frequency   Class   Branch
0       116.35      A       5
1       352         A       5
60      502.66      A       5
0       28          B       2   
1       15          B       2
1.5     19          B       2
22      21          B       2
0       96          C       6
0.99    45.5        C       6
1.1     32          C       6
1.99    66          C       4
35      105.8       C       4

I would like to get the maximum Frequency value for each Class and Branch. The desired output is:

Fitness Frequency   Class   Branch
60      502.66      A       5
0       28          B       2
0       96          C       6
35      105.8       C       4

I tried the following but didn't work:

df %>% group_by(Class, Branch) %>% top_n(n=1)

How can I do that?

Adam Amin
  • 1,406
  • 2
  • 11
  • 23

1 Answers1

1

You can use aggregate to get the max per Class and Branch.

aggregate(Frequency ~ Class + Branch, x, max)
#  Class Branch Frequency
#1     B      2     28.00
#2     C      4    105.80
#3     A      5    502.66
#4     C      6     96.00
GKi
  • 37,245
  • 2
  • 26
  • 48