title: Using dplyr::mutate between two dataframes to create column based on date range
body: <p>Right now I have two dataframes. One contains over 11 million rows of a start date, end date, and other variables. The second dataframe contains daily values for heating degree days (basically a temperature measure).</p>
  
  <pre><code>set.seed(1)    
  library(lubridate)
  date.range &lt;- ymd(paste(2008,3,1:31,sep="-"))
  daily &lt;- data.frame(date=date.range,value=runif(31,min=0,max=45))
  intervals &lt;- data.frame(start=daily$date[1:5],end=daily$date[c(6,9,15,24,31)])
  </code></pre>
  
  <p>In reality my daily dataframe has every day for 9 years and my intervals dataframe has entries that span over arbitrary dates in this time period.  What I wanted to do was to add a column to my <code>intervals</code> dataframe called <code>nhdd</code> that summed over the values in daily corresponding to that time interval (end exclusive).</p>
  
  <p>For example, in this case the first entry of this new column would be </p>
  
  <pre><code>sum(daily$value[1:5])
  </code></pre>
  
  <p>and the second would be</p>
  
  <pre><code>sum(daily$value[2:8]) and so on.
  </code></pre>
  
  <p>I tried using the following code</p>
  
  <pre><code>intervals &lt;- mutate(intervals,nhdd=sum(filter(daily,date&gt;=start&amp;date&lt;end)$value))
  </code></pre>
  
  <p>This is not working and I think it might have something to do with not referencing the columns correctly but I'm not sure where to go.</p>
  
  <p>I'd really like to use <code>dplyr</code> to solve this and not a loop because 11 million rows will take long enough using dplyr.  I tried using more of <code>lubridate</code> but dplyr doesn't seem to support the Period class.</p>
  
  <p>Edit: I'm actually using dates from <code>as.Date</code> now instead of <code>lubridate</code>but the basic question of how to refer to a different dataframe from within <code>mutate</code> still stands</p>