title: Dynamic calculation of difference between 2 rows in R using values from 1st 6 columns
body: <p>I have a data with 8 rows and 41 columns. The columns headers are time series from 8/1/2015-12/1/2018 and the rows are from 7/1/2015-2/1/2016.</p>
  
  <p>I am creating a new table where the ---
  1st row would be difference of first 2 rows.Each row would comprise sum of values of columns from 9/1/2015-2/1/2016. 
  For 2nd row the calculation would be difference of 2nd and 3rd row. Each row would comprise sum of values of columns from 10/1/2015-3/1/2016.
  For 3rd row it would be difference of 3rd and 4th row. Each row would comprise sum of values of columns from 11/1/2015-4/1/2016.</p>
  
  <pre><code>Column       Row       values
  8/1/2015    7/1/2015    25
  
  9/1/2015    7/1/2015    20
  
  10/1/2015   7/1/2015    22
  
  11/1/2015   7/1/2015    13
  
  12/1/2015   7/1/2015    16
  
  1/1/2016    7/1/2015    18
  
  2/1/2016    7/1/2015    10
  
  3/1/2016    7/1/2015    23
  
  4/1/2016    7/1/2015    14
  
  5/1/2016    7/1/2015    32
  
  6/1/2016    7/1/2015    17
  
  7/1/2016    7/1/2015    11
  
  8/1/2016    7/1/2015    12
  
  9/1/2016    7/1/2015    19
  
  10/1/2016   7/1/2015    20
  
  11/1/2016   7/1/2015    33
  
  12/1/2016   7/1/2015    20
  
  8/1/2015    8/1/2015    14
  
  9/1/2015    8/1/2015    25
  
  10/1/2015   8/1/2015    19
  
  11/1/2015   8/1/2015    18
  
  12/1/2015   8/1/2015    22
  
  1/1/2016    8/1/2015    20
  
  2/1/2016    8/1/2015    17
  
  3/1/2016    8/1/2015    18
  
  4/1/2016    8/1/2015    25
  
  5/1/2016    8/1/2015    23
  
  6/1/2016    8/1/2015    28
  
  7/1/2016    8/1/2015    22
  
  8/1/2016    8/1/2015    20
  
  9/1/2016    8/1/2015    24
  
  10/1/2016   8/1/2015    31
  
  11/1/2016   8/1/2015    27
  
  12/1/2016   8/1/2015    24
  </code></pre>
  
  <p>Similarly the calculation needs to continue from 3/1/2016-8/1/2016 and so on.</p>
  
  <p>Thanks</p>
  
  <p>Code that I have used</p>
  
  <pre><code>newdata2_reshape &lt;- reshape(newdata2,
                         timevar = c("Column"),
                         idvar = c("Row"),
                         direction = "wide")
  newdata2_reshape$date&lt;-newdata2_reshape$Row[1]
  
  newdata2_reshape$diff_in_days&lt;- difftime
  (newdata2_reshape$Row ,newdata2_reshape$date , units = c("days"))
  newdata2_reshape$months&lt;-ifelse(newdata2_reshape$diff_in_days &gt;=28 &amp; 
  newdata2_reshape$diff_in_days &lt;59,1,
  ifelse(newdata2_reshape$diff_in_days&gt;=59 &amp; 
  newdata2_reshape$diff_in_days&lt;89,2,
  ifelse(newdata2_reshape$diff_in_days&gt;=89 
  &amp; newdata2_reshape$diff_in_days&lt;120,3,
  ifelse(newdata2_reshape$diff_in_days&gt;=120 &amp; 
  newdata2_reshape$diff_in_days&lt;150,4,
  ifelse (newdata2_reshape$diff_in_days&gt;=150 &amp;
  newdata2_reshape$diff_in_days&lt;181,5,
  
                                                                                                                                                              ifelse(newdata2_reshape$diff_in_days&gt;=181 &amp; newdata2_reshape$diff_in_days&lt;212,6,
  ifelse(newdata2_reshape$diff_in_days&gt;=212 &amp; newdata2_reshape$diff_in_days&lt;242,7,
  ifelse(newdata2_reshape$diff_in_days&gt;=242 &amp; newdata2_reshape$diff_in_days&lt;273,8,
  ifelse(newdata2_reshape$diff_in_days&gt;=273 &amp; newdata2_reshape$diff_in_days&lt;303,9,
  ifelse(newdata2_reshape$diff_in_days&gt;=303 newdata2_reshape$diff_in_days&lt;334,10,
  ifelse(newdata2_reshape$diff_in_days&gt;=334 newdata2_reshape$diff_in_days &lt;365,11,
   ifelse (newdata2_reshape$diff_in_days&gt;=365,12,0))))))))))))
  
                                                                                           Now I have values against diff in days from 1 to 7 in my data
  </code></pre>
  
  <p>So if my value is 1,I should do a sum of 2nd to 7th column and subtract it from sum of 2nd to 7th column of previous row</p>
  
  <p>if my value for date is 2 then I should do a sum of 3rd to 8th column and subtract it from sum of 3rd to 8th column of previous row.</p>