rach
rach

Reputation: 141

Filter function

For the data:

    Id    res
    1     9
    1     8
    1     8
    1     6
    1     5
    1     4
    2     6
    2     6
    2     0
    2     0
    2     0
    2     0

I want top four from each group. When I use

dat %>% group_by(Id) %>%
       top_n(4,res)

I get

Id    res 
1     9 
1     8 
1     8 
1     6 
2     6
2     6 
2     0 
2     0 
2     0 
2     0

A filter method is required here. What would be a proper filter logic for this, so that I get only top four rows (ties are allowed)

Upvotes: 1

Views: 100

Answers (2)

Colonel Beauvel
Colonel Beauvel

Reputation: 31161

You can also simply use head, here with data.table package:

library(data.table)
setDT(df)[, head(res,4), Id]

Or as per @Jack Wheeler rightly underlined:

setDT(df)[, .SD[1:4], by = Id]

Upvotes: 1

jalapic
jalapic

Reputation: 14192

As far as I know top_n returns more rows if ties.

Maybe this helps?

dat %>% group_by(Id) %>% arrange(desc(res)) %>% filter(row_number()<=4)

  Id res
1  1   9
2  1   8
3  1   8
4  1   6
5  2   6
6  2   6
7  2   0
8  2   0

Upvotes: 5

Related Questions