I am using dplyr on R and I am trying to filter a tibble which contains transactional data.
The columns of my interest are "Country" and "Sales".
I have a lot of countries and for exploration purposes I want to analyze only the TOP 5 countries with most sales.
The trouble here is that if I do some grouping, it will not work for me, as I need all the rows for further analysis purposes (transactional data).
I tried something like:
trans_merch_df %>% group_by(COUNTRY) %>% top_n(n = 5, wt = NET_SLS_AMT)
But it's completely off.
Let's say I have this:
trans_merch_df <- tibble::tribble(~COUNTRY, ~SALE,
'POR', 14,
'POR', 1,
'DEU', 4,
'DEU', 6,
'POL', 8,
'ITA', 1,
'ITA', 1,
'ITA', 1,
'SPA', 1,
'NOR', 50,
'NOR', 10,
'SWE', 42,
'SWE', 1)
The result I am expecting is:
COUNTRY SALE
POR 14
POR 1
DEU 4
DEU 6
POL 8
NOR 50
NOR 10
SWE 42
SWE 1
As ITA and SPA are not in the TOP 5 of sales.
Thanks a lot in advance.
Cheers!
group_by
, think as if you have a separate little data frame for each group, and everything happens to each group and is then recombined. When yougroup_by() %>% top_n()
, you're pulling the the top rows within every group, not the top 5 groups. – Gregor Thomas