title: computing and formatting averages and squares of time intervals
body: <p>I have a model which predicts the duration of certain events, and measures of durations for those events. I then want to compute the difference between Predicted and Measured, the mean difference and the RMSE. I'm able to do it, but the formatting is really awkward and not what I expected:</p>
  
  <pre><code>database &lt;- data.frame(Predicted = c(strptime(c("4:00", "3:35", "3:38"), format = "%H:%M")),
                         Measured = c(strptime(c("3:39", "3:40", "3:53"), format = "%H:%M")))
  database
  &gt;             Predicted            Measured
  1 2016-11-28 04:00:00 2016-11-28 03:39:00
  2 2016-11-28 03:35:00 2016-11-28 03:40:00
  3 2016-11-28 03:38:00 2016-11-28 03:53:00
  </code></pre>
  
  <p>This is the first weirdness: why does R shows me a time <strong>and</strong> a date, even if I clearly specified a time-only format (<code>%H:%M</code>), and there was no date in my data to start with? It gets weirder:</p>
  
  <pre><code>database$Error &lt;- with(database, Predicted-Measured)
  database$Mean_Error &lt;- with(database, mean(Predicted-Measured))
  database$RMSE &lt;- with(database, sqrt(mean(as.numeric(Predicted-Measured)^2)))
  &gt; database
              Predicted            Measured    Error Mean_Error     RMSE
  1 2016-11-28 04:00:00 2016-11-28 03:39:00  21 mins  0.3333333 15.17674
  2 2016-11-28 03:35:00 2016-11-28 03:40:00  -5 mins  0.3333333 15.17674
  3 2016-11-28 03:38:00 2016-11-28 03:53:00 -15 mins  0.3333333 15.17674
  </code></pre>
  
  <p>Why is the variable <code>Error</code> expressed in minutes? For <code>Error</code> it's not a bad choice, but it becomes quite hard to read for <code>Mean_Error</code>. For <code>RMSE</code> it's even worse, but this could be due to the <code>as.numeric</code> function: if I remove it, R complains that <code>'^' not defined for "difftime" objects</code>. My questions are:</p>
  
  <ol>
  <li>Is it possible to show the first 2 columns (<code>Predicted</code> and <code>Measured</code>) shown in the <code>%H:%M</code> format?</li>
  <li>for the other 3 columns ( <code>Error</code>, <code>Mean_Error</code> and <code>RMSE</code>) I would like to compare a <code>%M:%S</code> format and a format in only seconds, and choose among the two. Is it possible? </li>
  </ol>
  
  <p><strong>EDIT</strong>: just to be more clear, my goal is to insert observations of time intervals into a dataframe and compute a vector of time interval differences. Then, compute some statistics for that vector: mean, RMSE, etc.. I know I could just enter the time observations in seconds, but that doesn't look very good: it's difficult to tell that 13200 seconds are 3 hours and 40 minutes. Thus I would like to be able to store the time intervals in the <code>%H:%M</code>, but then be able to manipulate them algebraically and show the results in a format of my choosing. Is that possible?</p>