title: Using dplyr to mutate() contract duration, payment counts, and total payout
body: <p>A simplified structure of my data is as follows:</p>
  
  <pre><code>&gt;ID &lt;- c("A", "B", "B", "C", "A", "B", "C", "C", "A", "B")
  &gt;Date = seq(as.Date("2000/07/01"), as.Date("2000/07/10"), "days")
  &gt;Amt &lt;- rnorm(10, 10, 3)
  
  &gt;E &lt;- data.frame(Date = Date, ID = ID, Amt = Amt)
  &gt;E
           Date ID  Amt
  1  2000-07-01  A  5.9
  2  2000-07-02  B  8.2
  3  2000-07-03  B  5.2
  4  2000-07-04  C 16.0
  5  2000-07-05  A  9.6
  6  2000-07-06  B  7.8
  7  2000-07-07  C  9.3
  8  2000-07-08  C  9.8
  9  2000-07-09  A 10.0
  10 2000-07-10  B 12.0
  </code></pre>
  
  <p>The structure of the <em>actual</em> data, which covers 14,460 payments on 1864 contracts at 1830 locations over 15 years (an irregularly-spaced, daily series) :</p>
  
  <pre><code>&gt; str(exp)
  'data.frame':   14460 obs. of  14 variables (not all shown):
   $ ID            : Factor w/ 1864 levels "C10677","C10717"... 
   $ Loc           : Factor w/ 1830 levels "(BLANK CREEK BRIDGE)" ...
   $ Year          : int  2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 ...
   $ Month         : int  7 7 7 7 7 7 7 7 7 7 ...
   $ Day           : int  5 6 6 7 11 11 12 16 16 16 ...
   $ MonthCount    : int  20 20 20 20 20 20 20 20 20 20 ...
   $ MonthTotal    : num  10140428 10140428 10140428 10140428 10140428 ...
   $ Date          : Date, format: "2000-07-05" "2000-07-06" "2000-07-06"...
  </code></pre>
  
  <p>My goal is the automation of the data ingestion task for the purposes of building a budget forecast that will live in a ShinyR application (far in the future). I used a SQL query to build the <code>$MonthCount</code> variable before I was better acquainted with the dplyr library (thanks to DataCamp), and now would like to know:</p>
  
  <p><strong>What is the most efficient way to use the dplyr library to (mutate?) the following features:</strong></p>
  
  <p>1) <em>"Contract Length"</em>: takes the difference between the <code>Date</code> of the last and first payments on a given contract <code>ID</code> and returns a <code>numeric</code> type.</p>
  
  <p>2) <em>"Contract Payment Count"</em>: counts how many payments were made on a given contract <code>ID</code> (presumably over the <code>Contract Length</code>). Assuming non-zero payments, this should = the count of how many times an <code>ID</code> appears in that column.</p>
  
  <p>3) <em>"Contract Payment Total"</em>: sums the <code>Amt</code> of each payment made to a given <code>ID</code>, (again, presumably over the realization I'm calling <code>Contract Length</code>).  </p>
  
  <p>Am I correct in thinking a single <code>mutate()</code> call would be able to handle the creation of all three of these features?</p>
  
  <p>EDIT: </p>
  
  <p>Having just picked up dplyr, I am chipping away at #1 with <code>E1 &lt;- mutate(E, ContractLength = max(Date) - min(Date))</code> but this returns a column of '9 days' since I'm not properly identifying a 'for each ID' clause...</p>