r - how to find top N descending values in group in dplyr -
i have following dataframe in r
serivce codes abs rt abs rt abs ty abs dr abs dr abs dr abs dr def rt def rt def ty def dr def dr def dr def dr def ty def se def se
what want service wise code count in descending order
serivce codes count abs dr 4 abs rt 2 abs ty 1 def dr 4 def rt 2 def ty 2
i doing following in r
df%>% group_by(service,codes) %>% summarise(count = n()) %>% top_n(n=3,wt = count) %>% arrange(desc(count)) %>% as.data.frame()
but,it not give me intended.
we can try count/arrange/slice
df1 %>% count(service, codes) %>% arrange(desc(n)) %>% group_by(service) %>% slice(seq_len(3)) # tibble: 6 x 3 # groups: service [2] # service codes n # <chr> <chr> <int> #1 abs dr 4 #2 abs rt 2 #3 abs ty 1 #4 def dr 4 #5 def rt 2 #6 def se 2
in op's code, need arrange
'service' too. @marius said in comments, top_n
include more number of rows if there ties. 1 option second grouping 'service' , slice
(as showed above) or after grouping, can filter
df1 %>% group_by(service,codes) %>% summarise(count = n()) %>% top_n(n=3,wt = count) %>% arrange(service, desc(count)) %>% group_by(service) %>% filter(row_number() <=3)
Comments
Post a Comment