My code is as follows:
kazex5 <- kazex4 %>%
select(YEAR, Product, Export, Product2, Export2) %>%
group_by(YEAR, Product2) %>%
arrange(YEAR, -Export2, -Export) %>%
top_n(3, wt = Product2)
But, it does not give the desired slicing of data based on column Product2
.
My data is as follows:
structure(list(YEAR = c(2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L), Product = c("Petroleum oils, oils from bitumin. materials, crude",
"Copper", "Pig iron & spiegeleisen, sponge iron, powder & granu",
"Petroleum oils or bituminous minerals > 70 % oil", "Wheat (including spelt) and meslin, unmilled",
"Flat-rolled prod., iron, non-alloy steel, not coated", "Zinc",
"Radio-actives and associated materials", "Iron ore and concentrates",
"Natural gas, whether or not liquefied", "Flat-rolled prod., iron, non-alloy steel, coated, clad",
"Ores and concentrates of base metals, n.e.s.", "Aluminium ores and concentrates (incl. alumina)",
"Coal, whether or not pulverized, not agglomerated", "Liquefied propane and butane",
"Aircraft & associated equipment; spacecraft, etc.", "Gold, non-monetary (excluding gold ores and concentrates)",
"Leather", "Meal and flour of wheat and flour of meslin", "Ferrous waste, scrape; remelting ingots, iron, steel",
"Ingots, primary forms, of iron or steel; semi-finis.", "Silver, platinum, other metals of the platinum group",
"Residual petroleum products, n.e.s., related mater.", "Copper ores and concentrates; copper mattes, cemen",
"Lead", "Cotton", "Inorganic chemical elements, oxides & halogen salts",
"Miscellaneous no-ferrous base metals for metallur.", "Barley, unmilled",
"Ball or roller bearings", "Tubes, pipes & hollow profiles, fittings, iron, steel",
"Wool and other animal hair (incl. wool tops)", "Other crude minerals",
"Fish, fresh (live or dead), chilled or frozen", "Sulphur and unroasted iron pyrites",
"Ships, boats & floating structures", "Vegetables", "Manufactures of base metal, n.e.s.",
"Pigments, paints, varnishes and related materials", "Fruits and nuts (excluding oil nuts), fresh or dried",
"Electrical machinery & apparatus, n.e.s.", "Electric current",
"Metallic salts & peroxysalts, of inorganic acids", "Fertilizers (other than those of group 272)",
"Other inorganic chemicals", "Flat-rolled products of alloy steel",
"Tobacco, manufactured", "Petroleum gases, other gaseous hydrocarbons, n.e.s.",
"Civil engineering & contractors' plant & equipment", "Feeding stuff for animals (no unmilled cereals)"
), Export = c(28125875.547, 2774753.829, 1420761.85, 1372686.455,
1170506.955, 1030047.529, 1009023.697, 861280.967, 795650.888,
654776.104, 577914.585, 531876.982, 522493.466, 521783.504, 460815.451,
457163.028, 398898.941, 345676.251, 339222.406, 338749.019, 327587.704,
323202.372, 279972.276, 260366.854, 244857.889, 183686.003, 183079.171,
156656.264, 111366.105, 96351.945, 87312.682, 84810.818, 81225.326,
75315.252, 69763.087, 68704.15, 60793.747, 56920.456, 54593.508,
53949.055, 52598.13, 50108.263, 45667.071, 40731.426, 39928.831,
39790.575, 35430.679, 33444.216, 32675.558, 32171.988), Product2 = c("Petroleum",
"Copper", "Iron_Steel", "Petroleum", "Wheat_Flour", "Iron_Steel",
"Zinc", "Radioactives", "Iron_Steel", "Natural_Gas", "Iron_Steel",
"Ores_metals", "Ores_metals", "Coal", "LPG", "Machinery", "Ores_metals",
"Leather", "Wheat_Flour", "Iron_Steel", "Iron_Steel", "Jewellery_Bullion",
"Petroleum", "Ores_metals", "Lead", "Cotton", "Chemicals", "Miscellaneous",
"Barley", "Iron_Steel", "Machinery", "Meat_Animals", "Petroleum",
"Fish", "Iron_Steel", "Vehicles", "Vegetables", "Ores_metals",
"Chemicals", "Fruits", "Machinery", "Electric current", "Chemicals",
"Chemicals", "Chemicals", "Iron_Steel", "Tobacco", "Petroleum",
"Vehicles", "Meat_Animals"), Export2 = c(130828, 24150, 205281,
130828, 88585, 205281, 24150, 24150, 205281, 24150, 205281, 136835,
136835, 36220, 24150, 90553, 136835, 8034, 88585, 205281, 205281,
30199, 130828, 136835, 24150, 24150, 70402, 24150, 24150, 205281,
90553, 24138, 130828, 24150, 205281, 40248, 12075, 136835, 70402,
10059, 90553, 16105, 70402, 70402, 70402, 205281, 22142, 130828,
40248, 24138)), row.names = c(NA, -50L), class = c("tbl_df",
"tbl", "data.frame"))