I have the data about sales by years and by-products, let's say like this:
Year <- c(2010,2010,2010,2010,2010,2011,2011,2011,2011,2011,2012,2012,2012,2012,2012)
Model <- c("a","b","c","d","e","a","b","c","d","e","a","b","c","d","e")
Sale <- c("30","45","23","33","24","11","56","19","45","56","33","32","89","33","12")
df <- data.frame(Year, Model, Sale)
product by years:
a= 30+11+33 = 74 b= 45+56+32 = 133 c= 23+19+89 = 131 d= 33+45+33 = 111 e= 12+56+24 = 92
Ranking by according to total sales within these 3 years:
1 2 3 4 5
b c d e a
I want the code which identifies the TOP 2 products (according to total sales within these 3 years) by years and summarises all the rest products as category "other". So the output should be like this:
year Model Sale 2010 b 45 2010 c 23 2010 other 30+33+24=92 2011 b 56 2011 c 19 2011 other 11+45+56=112 2012 b 32 2012 c 89 2012 other 33+33+12= 78