title: Lookup Last and First Value for unique id based on datetime in R
body: <p>I have below mentioned dataframe:</p>
  
  <pre><code>ID       Date                 Value1    Value2     Value3      Value4
  XX-12    2018-02-01 15:48:15  XXC       1000       15.45       18
  XX-12    2018-02-05 20:18:43  XTR       1500       15.45       12
  XX-13    2018-02-03 19:14:17  XRR       1900       18.25       10
  XX-13    2018-02-03 22:42:18  XTC       1600       20.25       12
  XX-14    2018-02-04 23:14:45  XRY       1100       10.50       10
  XX-15    2018-02-05 21:16:48  XTC       1400       20.25       14
  </code></pre>
  
  <p>From the above dataframe, I want to derive Initial value (<code>I_Value</code>) and final value (<code>F_Value</code>) based on  Datetime and difference between initial and final value.</p>
  
  <p>Required Output:</p>
  
  <pre><code>ID      I_Value1    F_Value1   I_Value2   F_Value2   Diff2   I_vaule3   F_Value3   Diff3   I_Value4    F_Value4   Diff4
  XX-12   XXC         XTR        1000       1500       500     15.45      15.45      0       18          12         -6
  XX-13   XRR         XTC        1900       1600      -300     18.25      20.25      2       10          12          2
  XX-14   XRY         XTC        1100       1100       0       10.50      10.50      0       10          10          0
  XX-15   XTC         XTC        1400       1400       0       20.25      20.25      0       14          14          0
  </code></pre>