title: Calculating relative time series in R
body: <p>I have a dataframe (example below) which I want to pass to a function which will take absolute time values (which are in POSIXct format) and turn them into a relative time series.</p>
  
  <pre><code>ID          Observation
  M97         11/09/2013 10:35
  M97         11/09/2013 13:13
  </code></pre>
  
  <p>I'm not that experienced with R functions so am struggling to get my head round writing it. I've got some pseudocode to start (shown below) to give the concept of what I want to do.</p>
  
  <pre><code>relative_time &lt;- function(df) {
      #for each row in the dataframe:
          #subtract the Observation value from the Observation value of the first row
          #Append the returned value to the end of the row in a new column called RelativeTime
  }
  </code></pre>
  
  <p>The output I want to achieve is as follows:</p>
  
  <pre><code>ID          Observation         RelativeTime
  M97         11/09/2013 10:35    0
  M97         11/09/2013 13:13    148
  </code></pre>
  
  <p>I've looked at <a href="https://stackoverflow.com/questions/3539916/relative-time-series">this question</a> amongst other things. But am stuck because the R syntax of a "for loop" is throwing me.</p>
  
  <p>Any help is much appreciated!</p>