2

Apologies, if this is a duplicate please let me know, I'll gladly delete.

I am attempting to select the four highest values for different values of another column.

Dataset:

   A   B   COUNT
1  1   2     2
2  1   3     6
3  1   4     3
4  1   5     9
5  1   6     2
6  1   7     7
7  1   8     0
8  1   9     5
9  1   10    2
10 1   11    7
11 2   1     5
12 2   3     1
13 2   4     8
14 2   5     9
15 2   6     5
16 2   7     2
17 2   8     2
18 2   9     4
19 3   1     7
20 3   2     5
21 3   4     2
22 3   5     8
23 3   6     6
24 3   7     1
25 3   8     9
26 3   9     5
27 4   1     8
28 4   2     1
29 4   3     1
30 4   5     3
31 4   6     9

For example, I would like to select four highest counts when A=1 (9,7,7,6) then when A=2 (9,8,5,5) and so on...

I would also like the corresponding B column value to be beside each count, so for when A=1 my desired output would be something like:

B   A   Count
5   1     9
7   1     7
11  1     7
3   1     6

I have looked a various answers on 'selecting highest values' but was struggling to find an example conditioning on other columns.

Many thanks

Joseph0210
  • 195
  • 7

2 Answers2

2

We can do

df1 %>%
     group_by(A) %>% 
     arrange(desc(COUNT)) %>%
     filter(row_number() <5)
akrun
  • 874,273
  • 37
  • 540
  • 662
2
library(dplyr)
data %>% group_by(A) %>% 
  arrange(A, desc(COUNT)) %>% 
  slice(1:4)
Patrik_P
  • 3,066
  • 3
  • 22
  • 39