title: Add Group By in For-Loop
body: <p>I have a dataset as follows: </p>
  
  <pre><code># Define Adstock Rate
  adstock_rate = 0.50
  
  # Create Data
  advertising = c(117.913, 120.112, 125.828, 115.354, 177.090, 141.647, 137.892,   0.000,   0.000,   0.000,   0.000, 
              0.000,   0.000,   0.000,   0.000,   0.000,   0.000, 158.511, 109.385,  91.084,  79.253, 102.706, 
              78.494, 135.114, 114.549,  87.337, 107.829, 125.020,  82.956,  60.813,  83.149,   0.000,   0.000, 
              0.000,   0.000,   0.000,   0.000, 129.515, 105.486, 111.494, 107.099,   0.000,   0.000,   0.000, 
              0.000,   0.000,   0.000,   0.000,   0.000,   0.000,   0.000,   0.000,
              134.913, 123.112, 178.828, 112.354, 100.090, 167.647, 177.892,   0.000,   0.000,   0.000,   0.000, 
              0.000,   0.000,   0.000,   0.000,   0.000,   0.000, 112.511, 155.385,  123.084,  89.253, 67.706, 
              23.494, 122.114, 112.549,  65.337, 134.829, 123.020,  81.956,  23.813,  65.149,   0.000,   0.000, 
              0.000,   0.000,   0.000,   0.000, 145.515, 154.486, 121.494, 117.099,   0.000,   0.000,   0.000, 
              0.000,   0.000,   0.000,   0.000,   0.000,   0.000,   0.000,   0.000
              )
  
  Region = c(500, 500, 500, 500, 500, 500, 500, 500,500, 500, 500, 500,500, 500, 500, 500,500, 500, 500, 500,500, 500, 500, 500,
         500, 500, 500, 500,500, 500, 500, 500,500, 500, 500, 500,500, 500, 500, 500,500, 500, 500, 500,500, 500, 500, 500, 500, 500, 
         500, 500,
         501, 501, 501, 501, 501, 501, 501, 501,501, 501, 501, 501,501, 501, 501, 501,501, 501, 501, 501,501, 501, 501, 501,
         501, 501, 501, 501,501, 501, 501, 501,501, 501, 501, 501,501, 501, 501, 501,501, 501, 501, 501,501, 501, 501, 501, 501, 501, 
         501, 501)
  
  advertising_dataset&lt;-data.frame(cbind(Region, advertising))
  </code></pre>
  
  <p>This is what the dataset looks like: </p>
  
  <pre><code>   Region advertising
  1     500     117.913
  2     500     120.112
  3     500     125.828
  4     500     115.354
  5     500     177.090
  6     500     141.647
  7     500     137.892
  8     500       0.000
  9     500       0.000
  10    500       0.000
  11    500       0.000
  12    500       0.000
  13    500       0.000
  14    500       0.000
  15    500       0.000
  16    500       0.000
  17    500       0.000
  18    500     158.511
  19    500     109.385
  20    500      91.084
  </code></pre>
  
  <p>From here, I am applying a lag function in which I take the first value, and then apply a for-loop to transform my dataset. </p>
  
  <pre><code># Alternative Method Using Loops Proposed by Linh Tran
  advertising_dataset$adstocked_advertising = numeric(length(advertising_dataset$advertising))
  advertising_dataset$adstocked_advertising[1] = advertising_dataset$advertising[1]
  
  for(i in 2:length(advertising_dataset$advertising)){
    advertising_dataset$adstocked_advertising[i] = advertising_dataset$advertising[i] + adstock_rate * advertising_dataset$adstocked_advertising[i-1]}
  </code></pre>
  
  <p>The issue I am having is that my dataset is separated by Region. I need to apply this function above (including taking the first value) by region. </p>
  
  <p>Is there a way to do this with the dplyr package? </p>
  
  <p>I know this is wrong but maybe something like this: </p>
  
  <pre><code>library(dplyr)
  separated_by_region&lt;- advertising_dataset %&gt;%
  group_by(Region) %&gt;%
  summarise(
  advertising_dataset$adstocked_advertising = 
  numeric(length(advertising_dataset$advertising))
  advertising_dataset$adstocked_advertising[1] = 
  advertising_dataset$advertising[1]
  
  for(i in 2:length(advertising_dataset$advertising)){
    advertising_dataset$adstocked_advertising[i] = 
  advertising_dataset$advertising[i] + adstock_rate * 
  advertising_dataset$adstocked_advertising[i-1]})
  </code></pre>
  
  <p>Something along these lines. Not really sure how to do this. </p>
  
  <p>I have a feeling I may have to use the split(advertising_dataset, advertising_dataset$Region) and use an apply function and rbind the results. </p>
  
  <p>Any help would be great, thanks!</p>
  
  <p>Example Output (but function needs to be applied by Region) 1 final dataset in the end: </p>
  
  <pre><code>  Region     advertising     adstocked_advertising
       500         117.913               117.9130000
       500         120.112               179.0685000
       500         125.828               215.3622500
       500         115.354               223.0351250
       500         177.090               288.6075625
       500         141.647               285.9507812
       500         137.892               280.8673906
       500           0.000               140.4336953
       500           0.000                70.2168477
       500           0.000                35.1084238
       500           0.000                17.5542119
       500           0.000                 8.7771060
       500           0.000                 4.3885530
       500           0.000                 2.1942765
       500           0.000                 1.0971382
       500           0.000                 0.5485691
       500           0.000                 0.2742846
       500         158.511               158.6481423
       500         109.385               188.7090711
       500          91.084               185.4385356
  </code></pre>