title: Subsetting dataframes with loop - failing
body: <p>I have a loop that's causing me not only time problems as it's taking a long time to run on a dataset of 30k, but not providing the correct output. </p>
  
  <pre><code>interval &lt;- days(10)
  
  df &lt;- data.frame(CompanyID = c(23512, 51250, 12515, 51250, 52512, 51250), 
                   openingDate = as.Date(c("1999-07-15", "1995-02-01", 
                   "2001-01-25", "1995-02-04", "2004-07-08", "1996-05-25")),
                   Rev = c(1000, 2000, 4000, 5000, 5500, 2050))
  
  for(id in unique(df$CompanyID)){
      df[, c("groupID")] &lt;- NA
      df[, c("updatedRev")] &lt;- df$Rev
  
  tempDF &lt;- df %&gt;% filter(CompanyID == id)
  
  if(nrow(tempDF) == 1){
    #arbitrary unique ID
    df$groupID[df$CompanyID == id] &lt;- paste(id) 
  }
  
  else{
    dfDateRange &lt;- tempDF[order(tempDF$openingDate),]
  
    while(nrow(dfDateRange) &gt; 0) { # until table is empty
  
      earliest_date &lt;- dfDateRange$openingDate[1] #earliest date within subset
      within_dates &lt;- dfDateRange %&gt;% filter(openingDate &lt;= earliest_date + interval) 
      # all data within the interval
  
      # values to replace in subset that falls within date range
      # Revenue
      df$updatedRev[(df$CompanyID == id) &amp;
                      (df$openingDate %in% within_dates$openingDate )] &lt;-
      sum(within_dates$Rev)
  
      # Generate Group ID 
      df$groupID[(df$CompanyID == id) &amp;
                   (df$openingDate %in% within_dates$openingDate)] &lt;- paste(id)
  
      # moving on to rows outside of date range
      dfDateRange &lt;- dfDateRange %&gt;% 
         filter(dfDateRange$openingDate &gt; (earliest_date + interval))
  
    }
   }
  }  
  </code></pre>
  
  <p>The translation of this code is:</p>
  
  <ul>
  <li>Take in a large dataframe, create a temp group ID column to be used later and a updatedRev column that will be overwritten later dependent on conditions</li>
  <li>For every unique CompanyID, create a dataframe and assign a new ID</li>
  <li>If there are multiple incidences of CompanyID, then we subset on the condition that all incidences fall into a certain date range</li>
  <li>For those that fall into the specified date range, we'll override Revenue values and assign a group ID to that group (in this example, rows 2 and 4)</li>
  <li>For those that fall outside the date range, go to the next earliest opening date - and see which rows fall within that range and follow steps beginning from the for loop</li>
  </ul>
  
  <p>Two questions about this. When I return the dataframe after running the loop, I get back </p>
  
  <pre><code>&gt; df
    CompanyID openingDate  Rev groupID updatedRev
  1     23512  1999-07-15 1000    &lt;NA&gt;       1000
  2     51250  1995-02-01 2000    &lt;NA&gt;       2000
  3     12515  2001-01-25 4000    &lt;NA&gt;       4000
  4     51250  1995-02-04 5000    &lt;NA&gt;       5000
  5     52512  2004-07-08 5500   52512       5500
  6     51250  1996-05-25 2050    &lt;NA&gt;       2050
  </code></pre>
  
  <p>When I should get back:</p>
  
  <pre><code>  CompanyID openingDate  Rev groupID updatedRev
  1     23512  1999-07-15 1000   23512       1000
  2     51250  1995-02-01 2000   51250       7000
  3     12515  2001-01-25 4000   12515       4000
  4     51250  1995-02-04 5000   51250       7000
  5     52512  2004-07-08 5500   52512       5500
  6     51250  1996-05-25 2050   51250       2050
  </code></pre>
  
  <p>*note, the updatedRev did not update to replace values with summed values of duplicates CompanyID's 51250 that fall into a 10 day range of each other.</p>
  
  <p>My other question, when I wrap this around in a function - say <code>test()</code> and run the function <code>test(df)</code> - <code>df</code> does not change. Why is that? </p>
  
  <p>Note: I will be adjusting the groupId later to be more accurate, so that row 6 can be differentiated between Row 2 &amp; 4. Just want to get this question up as that's just a line adjustment. </p>