title: R identifying first value in data-frame and creating new variable by adding/subtracting this from all values in data-frame in new column
body: <p>I know this question may have been already answered elsewhere and apologies for repeating it if so but I haven't found a workable answer as yet. </p>
  
  <p>I have 17 subjects each with two variables as below:</p>
  
  <pre><code>Time (s)    OD
  130        41.48
  130.5      41.41
  131        39.6
  131.5      39.18
  132        39.41
  132.5      37.91
  133        37.95
  133.5      37.15
  134        35.5
  134.5      36.01
  135        35.01
  </code></pre>
  
  <p>I would like R to identify the first value in column 2 (OD) of my dataframe and create a new column (OD_adjusted) by adding or subtracting (depending if the first value is +ive or -ive) from all values in column 2 so it would look like this: </p>
  
  <pre><code>    Time (s)    OD      OD_adjusted 
  130            41.48     0
  130.5          41.41    -0.07
  131            39.6     -1.88
  131.5          39.18    -2.3
  132            39.41    -2.07
  132.5          37.91    -3.57
  133            37.95    -3.53
  133.5          37.15    -4.33
  134            35.5     -5.98
  134.5          36.01    -5.47
  135            35.01    -6.47
  </code></pre>
  
  <p>First value in column 2 is 41.48 therefore I want to subtract this value from all datapoints in column 2 to create a new third column (OD_adjusted).</p>
  
  <p>I can use <code>OD_adjusted &lt;- ((df$OD) - 41.48)</code> however, I would like to automate the process using a function and this is where I am stuck:</p>
  
  <pre><code>AUC_OD &lt;- function(df){
  
  return_value_1 = df %&gt;%
    arrange(OD) %&gt;%
    filter(OD [1,2) %&gt;%
    slice_(1)
  colnames(return_value_1)[3] &lt;- "OD_adjusted"
  
  
  if (nrow(return_value_1) &gt; 0 ) { subtract 
     (return_value_1 [1,2] #into new row
  
       else add
       (return_value_1 [1,2] #into new row
  
  }
  </code></pre>