title: How to loop a regression filtering for days
body: <p>I try to loop a regression, and filter for only using the dates between -20 and -10 prior to the i.</p>
  <p>Aspects:</p>
  <ol>
  <li>need to be grouped by company</li>
  <li>I want to run a regression for every date in the dataset -&gt; thats why I have added for() loop</li>
  <li>need to regress on date [-20,-10] with respect to the date in that row.</li>
  <li>save intercept an beta in a new data frame (including date and company variable)</li>
  </ol>
  <p>I run the following code:</p>
  <pre><code>stocks %&gt;%
    group_by(Company) %&gt;%
    for i:length(stocks$date){ 
      lm(Return~indexfactor, filter(stocks$date==c(i-20:i-10)))
      }
   
  </code></pre>
  <p>But get error that looks like this.. Is there someone better in R than me that could help me out here?</p>
  <pre><code>Error: unexpected symbol in:
  &quot;  group_by(Company) %&gt;%
    for i&quot;
  &gt;     lm(Return~indexfactor, filter(stocks$date==c(i-50:i-10)))
  Error in filter(stocks$date == c(i - 50:i - 10)) : object 'i' not found
  &gt;     }
  Error: unexpected '}' in &quot;    }&quot;
  </code></pre>
  <p>head(stocks)</p>
  <pre><code>   Company      Return       date indexfactor
  1:    TSLA          NA 2020-01-02           1
  2:    TSLA  0.02963319 2020-01-03           2
  3:    TSLA  0.01925467 2020-01-06           3
  4:    TSLA  0.03880052 2020-01-07           1
  5:    TSLA  0.04920485 2020-01-08           2
  6:    TSLA -0.02194501 2020-01-09           3
  </code></pre>
  <p>dput:</p>
  <pre><code>library(dplyr)
  
  structure(list(Company = c(&quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, 
  &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, 
  &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, 
  &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, 
  &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;TSLA&quot;, &quot;AAPL&quot;, 
  &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, 
  &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, 
  &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, 
  &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, 
  &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;AAPL&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, 
  &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, 
  &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, 
  &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, 
  &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;, &quot;MSFT&quot;), Return = structure(c(NA, 
  -0.00231580860313196, 0.00161115249502686, 0.0263778178160983, 
  0.00126177020327534, 0.00131321050919397, -0.0246270372908958, 
  0.00914064109251655, 0.00200537012113067, 0.0152319401903576, 
  0.00153470089029306, 0.00330255357802023, -0.0386048629818018, 
  0.0126135313891698, 0.016987201324169, 0.00226083596494164, 0.0275330390995761, 
  0.0219139834854407, -0.017438317910401, 0.00482277896193928, 
  -0.00746184998902322, -0.000335287830383391, 0.00813970000264619, 
  -0.00120277144206837, 0.00276710299297744, 0.0138741424465028, 
  -0.0102317198037775, 0.00821145028213532, 0.0174002689158261, 
  0.00665249242034749, 0.00892635726223312, 0.0301960379663551, 
  0.0321221724831346, -0.00671175495130083, 0.0017702430543638, 
  0.0325710468824816, NA, 0.00747491336551409, 0.004230452491915, 
  0.0154892997636558, -0.0100836108228525, -0.00670488036422623, 
  -0.0331018742668066, 0.0038934147553642, -0.00956207269350584, 
  0.00614369450930652, -0.00161045428175832, -0.0183479745173454, 
  -0.0213610573923191, 0.00342794874173857, 0.0168724333689398, 
  0.00671920438590856, 0.00061292650530631, -0.0105499797243396, 
  -0.0238012734333519, 0.00648296789562508, -0.00931178310842573, 
  0.00812716607773845, -0.0246056442175525, 0.0141584160856578, 
  0.00630712914529696, 0.00908445774647904, -0.00272174624225163, 
  -0.000629783091523284, -0.00910302512573391, -0.00423991959013226, 
  0.0202256115186827, 0.00751252808543867, 0.0118061795583038, 
  0.0150801225162718, 0.00336111869323474, 0.00147394484369379, 
  NA, -0.00225290073646667, -3.31363114687289e-05, -0.00318795893000134, 
  9.9933375639738e-05, -0.00985973514785521, -0.0051808544995795, 
  0.00432856189833641, 0.00942799115889392, 0.0167784043797137, 
  0.0013122301384898, -0.0175283598141395, -0.0185747393633032, 
  0.00169894672234916, 0.012822249504298, 0.00328223940876526, 
  -0.000701001473501162, -0.0173041352803581, -0.0362036357526352, 
  0.00169304112099122, -0.00732389788732402, 0.0254681919300281, 
  -0.0207195464395805, 0.0199569965714916, 0.0150643262548715, 
  0.00593640984287869, 0, -0.00210274711678327, -0.00458826751020991, 
  0.0117112569702396, 0.0217340016991259, 0.00482243104872015, 
  0.0101246444598198, 0.00305900606094878, -0.00266037365193483, 
  0.0108975178309423), .Dim = c(108L, 1L), .Dimnames = list(NULL, 
      &quot;Delt.1.arithmetic&quot;)), date = structure(c(18871, 18872, 18873, 
  18877, 18878, 18879, 18880, 18883, 18884, 18885, 18886, 18887, 
  18890, 18891, 18892, 18893, 18894, 18897, 18898, 18899, 18900, 
  18901, 18904, 18905, 18906, 18907, 18908, 18911, 18912, 18913, 
  18914, 18915, 18918, 18919, 18920, 18921, 18871, 18872, 18873, 
  18877, 18878, 18879, 18880, 18883, 18884, 18885, 18886, 18887, 
  18890, 18891, 18892, 18893, 18894, 18897, 18898, 18899, 18900, 
  18901, 18904, 18905, 18906, 18907, 18908, 18911, 18912, 18913, 
  18914, 18915, 18918, 18919, 18920, 18921, 18871, 18872, 18873, 
  18877, 18878, 18879, 18880, 18883, 18884, 18885, 18886, 18887, 
  18890, 18891, 18892, 18893, 18894, 18897, 18898, 18899, 18900, 
  18901, 18904, 18905, 18906, 18907, 18908, 18911, 18912, 18913, 
  18914, 18915, 18918, 18919, 18920, 18921), class = &quot;Date&quot;), indexfactor = c(1L, 
  2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
  3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
  1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
  2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
  3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
  1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
  2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L)), row.names = c(&quot;2021-09-01&quot;, 
  &quot;2021-09-02&quot;, &quot;2021-09-03&quot;, &quot;2021-09-07&quot;, &quot;2021-09-08&quot;, &quot;2021-09-09&quot;, 
  &quot;2021-09-10&quot;, &quot;2021-09-13&quot;, &quot;2021-09-14&quot;, &quot;2021-09-15&quot;, &quot;2021-09-16&quot;, 
  &quot;2021-09-17&quot;, &quot;2021-09-20&quot;, &quot;2021-09-21&quot;, &quot;2021-09-22&quot;, &quot;2021-09-23&quot;, 
  &quot;2021-09-24&quot;, &quot;2021-09-27&quot;, &quot;2021-09-28&quot;, &quot;2021-09-29&quot;, &quot;2021-09-30&quot;, 
  &quot;2021-10-01&quot;, &quot;2021-10-04&quot;, &quot;2021-10-05&quot;, &quot;2021-10-06&quot;, &quot;2021-10-07&quot;, 
  &quot;2021-10-08&quot;, &quot;2021-10-11&quot;, &quot;2021-10-12&quot;, &quot;2021-10-13&quot;, &quot;2021-10-14&quot;, 
  &quot;2021-10-15&quot;, &quot;2021-10-18&quot;, &quot;2021-10-19&quot;, &quot;2021-10-20&quot;, &quot;2021-10-21&quot;, 
  &quot;2021-09-011&quot;, &quot;2021-09-021&quot;, &quot;2021-09-031&quot;, &quot;2021-09-071&quot;, &quot;2021-09-081&quot;, 
  &quot;2021-09-091&quot;, &quot;2021-09-101&quot;, &quot;2021-09-131&quot;, &quot;2021-09-141&quot;, &quot;2021-09-151&quot;, 
  &quot;2021-09-161&quot;, &quot;2021-09-171&quot;, &quot;2021-09-201&quot;, &quot;2021-09-211&quot;, &quot;2021-09-221&quot;, 
  &quot;2021-09-231&quot;, &quot;2021-09-241&quot;, &quot;2021-09-271&quot;, &quot;2021-09-281&quot;, &quot;2021-09-291&quot;, 
  &quot;2021-09-301&quot;, &quot;2021-10-011&quot;, &quot;2021-10-041&quot;, &quot;2021-10-051&quot;, &quot;2021-10-061&quot;, 
  &quot;2021-10-071&quot;, &quot;2021-10-081&quot;, &quot;2021-10-111&quot;, &quot;2021-10-121&quot;, &quot;2021-10-131&quot;, 
  &quot;2021-10-141&quot;, &quot;2021-10-151&quot;, &quot;2021-10-181&quot;, &quot;2021-10-191&quot;, &quot;2021-10-201&quot;, 
  &quot;2021-10-211&quot;, &quot;2021-09-012&quot;, &quot;2021-09-022&quot;, &quot;2021-09-032&quot;, &quot;2021-09-072&quot;, 
  &quot;2021-09-082&quot;, &quot;2021-09-092&quot;, &quot;2021-09-102&quot;, &quot;2021-09-132&quot;, &quot;2021-09-142&quot;, 
  &quot;2021-09-152&quot;, &quot;2021-09-162&quot;, &quot;2021-09-172&quot;, &quot;2021-09-202&quot;, &quot;2021-09-212&quot;, 
  &quot;2021-09-222&quot;, &quot;2021-09-232&quot;, &quot;2021-09-242&quot;, &quot;2021-09-272&quot;, &quot;2021-09-282&quot;, 
  &quot;2021-09-292&quot;, &quot;2021-09-302&quot;, &quot;2021-10-012&quot;, &quot;2021-10-042&quot;, &quot;2021-10-052&quot;, 
  &quot;2021-10-062&quot;, &quot;2021-10-072&quot;, &quot;2021-10-082&quot;, &quot;2021-10-112&quot;, &quot;2021-10-122&quot;, 
  &quot;2021-10-132&quot;, &quot;2021-10-142&quot;, &quot;2021-10-152&quot;, &quot;2021-10-182&quot;, &quot;2021-10-192&quot;, 
  &quot;2021-10-202&quot;, &quot;2021-10-212&quot;), class = &quot;data.frame&quot;)
  </code></pre>