title: Subtracting values of a data frame based on a condition for another data frame
body: <p>I have two data frames with same dimension. One data frame (df1) consists of "1" and "0", while the other data frame (df2) has varying values. I want to create a new data frame (df3) by subtracting df2 values based on a condition. The condition is that whenever there is "1" in df1, this location should be identified in df2 (e.g. its row 1st, col 4th). Now value of df2 in the preceding column (row 1st, col 3rd) should be considered as a base {0.98}, and the values in next two columns (row 1st, col 4th {0.6} and col 5th {0.75}) should be subtracted one by one from this base value.
  It is explained in a sample below:</p>
  
  <pre><code>df1:
   ID    2005    2006    2007    2008    2009
   1      NA      NA      0       1       0
   2      NA      NA      0       1       1
   3       0      0       0       NA      0
   4       0      1       0       0       1
  
  df2:
   ID    2005    2006    2007    2008    2009
   1      NA     0.7     0.98     0.6     0.75
   2      NA     0.2     0.43     0.3     0.5
   3     0.1    -0.98    0.01     0.09    0.1
   4     0.05    -0.1    0.05     0.12    0.23
  </code></pre>
  
  <p>I want df3 to be as follows after subtraction:</p>
  
  <pre><code>df3:
   ID    2005    2006    2007    2008    2009
   1      NA      NA      0     -0.38    -0.23
   2      NA      NA      0     -0.13     0.07
   3      NA      NA      NA      NA      NA
   4      0     -0.15     0       0       0.11
  </code></pre>
  
  <p>Here row3 is all NA because there is no "1" in df1, so no subtraction.</p>
  
  <p>I want to use "which" function to identify the location but the subtraction from preceding column is making it a bit complicated for me. Your help will be highly appreciated.</p>
  
  <p>Thanks. Saba</p>