title: Looping monthly indices for a particular year
body: <p>I have a data format like attached image below where we are having sales figures of different SKUs for multiple years, distributed monthly.</p>
  
  <p><a href="https://i.stack.imgur.com/k5z6n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k5z6n.png" alt="Input file format"></a></p>
  
  <p>I have calculated monthly indices for each year using the function;</p>
  
  <pre><code>calc_monthly_all&lt;- function(X){
    X$sku &lt;- as.character(X$sku)
    X$Yearly_Avg &lt;- rowMeans(X[,-c(1:2)])
    level_one &lt;- X %&gt;% dplyr::mutate_at(vars(starts_with('Month_')), funs(./Yearly_Avg))
    return(level_one)
    }
  </code></pre>
  
  <p>The data displayed is read as 'df_data' in R, &amp; after applying the function, output is like below,</p>
  
  <pre><code>&gt; calc_monthly_all(df_data)
               sku Year  Month_Jan  Month_Feb Month_March Month_April Month_May Month_June Month_July Month_Aug Month_Sept
  1 10929000284004 2015 0.32601608 0.19821778   0.6755053   2.0838948 0.5398826  1.7657031  1.2414692 1.1136709  0.6650728
  2 10929000284004 2016 0.92209048 2.52811562   1.4961071   1.8380744 0.6534019  0.7511068  0.5190576 0.2992214  0.1038115
  3 10929001124004 2014 0.04678503 0.04318618   0.4847649   0.9385797 0.9781670  0.9353407  0.7924664 1.2894674  1.2070537
  4 10929001124004 2015 1.40699537 1.86480849   0.7979080   0.7520179 0.3975936  1.1719218  0.4233502 0.6419178  1.5749569
  5 10929001124004 2016 0.92209048 2.52811562   1.4961071   1.8380744 0.6534019  0.7511068  0.5190576 0.2992214  0.1038115
  6 10929001124104 2016 1.00160192 0.65078094   0.8191163   0.8358030 1.0112802  0.9419971  0.7209318 1.0449873  1.0337071
  7 10929001124104 2017 0.83334681 0.74955923   0.7739514   1.2059589 1.1626741  1.4993773  1.0948676 0.9872054  1.1436520
    Month_Oct Month_Nov Month_Dec Yearly_Avg
  1 1.5831341 1.1945229 0.6129102   3834.167
  2 1.2029922 0.5434838 1.1425373   1637.583
  3 2.2273273 1.7357246 1.3211372  27786.667
  4 0.9569818 0.6437317 1.3678164  55131.667
  5 1.2029922 0.5434838 1.1425373   1637.583
  6 1.3110399 1.3426111 1.2861434 149820.000
  7 0.9806706 0.8718438 0.6968927 154557.500
  </code></pre>
  
  <p>Now, for each year of each SKU, we got the monthly indices. Now we need to take the indices for one particular year, let's say for first year. That means for each SKU, we will take monthly indices for their respective first year, return the data frame containing only first year's monthly indices for each SKU. For that I tried;</p>
  
  <pre><code>calc_monthly_fys&lt;- function(X){
    X$sku &lt;- as.character(X$sku)
    X$Yearly_Avg &lt;- rowMeans(X[,-c(1:2)])
    level_one &lt;- X %&gt;% dplyr::mutate_at(vars(starts_with('Month_')), funs(./Yearly_Avg))
    first_yr_store &lt;- data.frame()
    for (i in unique(level_one$sku)){
      fys1 &lt;- subset(level_one,sku %in% i)
      fys &lt;- fys1[1,]
      df &lt;-data.frame(fys)
      df_total &lt;- rbind(first_yr_store,df)
      return(df_total)
    }
  } 
  </code></pre>
  
  <p>but, It's not giving proper result, only first SKU is appearing.</p>
  
  <pre><code>calc_monthly_fys(df_data)
               sku Year Month_Jan Month_Feb Month_March Month_April Month_May Month_June Month_July Month_Aug Month_Sept
  1 10929000284004 2015 0.3260161 0.1982178   0.6755053    2.083895 0.5398826   1.765703   1.241469  1.113671  0.6650728
    Month_Oct Month_Nov Month_Dec Yearly_Avg
  1  1.583134  1.194523 0.6129102   3834.167
  </code></pre>
  
  <p>I need this for all SKUs.</p>
  
  <p>Here the example is having 3 unique SKUs, but data may have 'n' number of SKU. Finally the output I need should be in below format;</p>
  
  <p><a href="https://i.stack.imgur.com/3lU80.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3lU80.png" alt="Final Table output"></a></p>
  
  <p>The names of the columns, (SKU_1 to SKU_N) should come dynamically (If we have four unique SKUs, that four SKU names should appear as the column names). <code>_FYI</code> indicates here 'First Year Index', I think we may use <code>paste</code> function to attach the suffix <code>_FYI</code> after each unique SKU name in the final output table.</p>
  
  <p>Please help me to generate monthly indices in the discussed format for N number of SKUs &amp; M number of years.</p>
  
  <p>TIA</p>